
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django 管理站点 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="管理动作" href="actions.html" />
    <link rel="prev" title="contrib 包" href="../index.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;contrib&lt;/span&gt;&lt;/code&gt; 包">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="actions.html" title="管理动作">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-admin-index">
            
  <div class="section" id="s-module-django.contrib.admin">
<span id="s-the-django-admin-site"></span><span id="module-django.contrib.admin"></span><span id="the-django-admin-site"></span><h1>Django 管理站点<a class="headerlink" href="#module-django.contrib.admin" title="永久链接至标题">¶</a></h1>
<p>Django 最强大的部分之一是自动管理界面。它从你的模型中读取元数据，提供一个快速的、以模型为中心的界面，受信任的用户可以管理你网站上的内容。管理的推荐使用范围仅限于一个组织的内部管理工具。它不打算用于围绕你的整个前端构建。</p>
<p>管理有很多用于定制的钩子，但要注意不要试图专门使用这些钩子。如果你需要提供一个更以流程为中心的接口，抽象掉数据库表和字段的实现细节，那么可能是时候编写自己的视图了。</p>
<p>在本文档中，我们将讨论如何激活、使用和定制 Django 的管理界面。</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>概况<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
<p>在 <a class="reference internal" href="../../django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">startproject</span></code></a> 使用的默认项目模板中启用了管理。</p>
<p>如果你没有使用默认的项目模板，这里是要求：</p>
<ol class="arabic">
<li><p class="first">将 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> 和它的依赖关系 —— <a class="reference internal" href="../../../topics/auth/index.html#module-django.contrib.auth" title="django.contrib.auth: Django's authentication framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.auth</span></code></a>、<a class="reference internal" href="../contenttypes.html#module-django.contrib.contenttypes" title="django.contrib.contenttypes: Provides generic interface to installed models."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.contenttypes</span></code></a>、<a class="reference internal" href="../messages.html#module-django.contrib.messages" title="django.contrib.messages: Provides cookie- and session-based temporary message storage."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.messages</span></code></a> 和 <code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.session</span></code> 添加到你的 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中。</p>
</li>
<li><p class="first">在你的 <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> 配置中设置一个在 <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES-OPTIONS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">OPTIONS</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">'context_processors'</span></code> 选项中包含 <code class="docutils literal notranslate"><span class="pre">django.template.context_processors.request</span></code>、<code class="docutils literal notranslate"><span class="pre">django.contrib.auth.context_processors.auth</span></code> 和 <code class="docutils literal notranslate"><span class="pre">django.contrib.messages.context_processors.messages</span></code> 的 <a class="reference internal" href="../../../topics/templates.html#django.template.backends.django.DjangoTemplates" title="django.template.backends.django.DjangoTemplates"><code class="xref py py-class docutils literal notranslate"><span class="pre">DjangoTemplates</span></code></a> 后端：</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p><code class="docutils literal notranslate"><span class="pre">django.template.context_processors.request</span></code> 被添加为 <code class="docutils literal notranslate"><span class="pre">'context_processors'</span></code> 选项中的依赖，以支持新的 <a class="reference internal" href="#django.contrib.admin.AdminSite.enable_nav_sidebar" title="django.contrib.admin.AdminSite.enable_nav_sidebar"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.enable_nav_sidebar</span></code></a>。</p>
</div>
</li>
<li><p class="first">如果你自定义了 <a class="reference internal" href="../../settings.html#std:setting-MIDDLEWARE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MIDDLEWARE</span></code></a> 设置，则必须包含 <a class="reference internal" href="../../middleware.html#django.contrib.auth.middleware.AuthenticationMiddleware" title="django.contrib.auth.middleware.AuthenticationMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.auth.middleware.AuthenticationMiddleware</span></code></a> 和 <a class="reference internal" href="../../middleware.html#django.contrib.messages.middleware.MessageMiddleware" title="django.contrib.messages.middleware.MessageMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.messages.middleware.MessageMiddleware</span></code></a>。</p>
</li>
<li><p class="first"><a class="reference internal" href="#hooking-adminsite-to-urlconf"><span class="std std-ref">把管理的网址挂到你的 URLconf 里</span></a>。</p>
</li>
</ol>
<p>在你采取了这些步骤之后，你就可以通过访问你挂接的 URL（默认是 <code class="docutils literal notranslate"><span class="pre">/admin/</span></code>）来使用管理站点。</p>
<p>如果需要创建一个用户来登录，请使用 <a class="reference internal" href="../../django-admin.html#django-admin-createsuperuser"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">createsuperuser</span></code></a> 命令。默认情况下，登录管理需要用户的 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_staff" title="django.contrib.auth.models.User.is_staff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_staff</span></code></a> 属性设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p>最后，确定你的应用程序的哪些模型应该在管理界面中是可编辑的。对于这些模型中的每一个，按照 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 中的描述，在管理处注册它们。</p>
<div class="section" id="s-other-topics">
<span id="other-topics"></span><h3>其他主题<a class="headerlink" href="#other-topics" title="永久链接至标题">¶</a></h3>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="actions.html">管理动作</a></li>
<li class="toctree-l1"><a class="reference internal" href="admindocs.html">Django 管理文档生成器</a></li>
<li class="toctree-l1"><a class="reference internal" href="javascript.html">管理的 JavaScript 自定义</a></li>
</ul>
</div>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p>关于在生产中服务与管理相关的静态文件（图像、JavaScript 和 CSS）的信息，请参见 <a class="reference internal" href="../../../howto/deployment/wsgi/modwsgi.html#serving-files"><span class="std std-ref">提供文件服务</span></a>。</p>
<p class="last">有问题？ 试试 <a class="reference internal" href="../../../faq/admin.html"><span class="doc">FAQ：管理</span></a>。</p>
</div>
</div>
</div>
<div class="section" id="s-modeladmin-objects">
<span id="modeladmin-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 对象<a class="headerlink" href="#modeladmin-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.ModelAdmin">
<em class="property">class </em><code class="descname">ModelAdmin</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 类是管理界面中模型的表示。通常，这些都存储在你的应用程序中一个名为 <code class="docutils literal notranslate"><span class="pre">admin.py</span></code> 的文件中。让我们来看看 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 的一个例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Author</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">AuthorAdmin</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-do-you-need-a-modeladmin-object-at-all admonition">
<p class="first admonition-title">你是否需要一个 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 对象？</p>
<p>在前面的例子中，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 类没有定义任何自定义值（还没有）。因此，将提供默认的管理界面。如果你对默认的管理界面满意，你根本不需要定义一个 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 对象 —— 你可以不提供 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 描述而注册模型类。前面的例子可以简化为：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Author</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">)</span>
</pre></div>
</div>
</div>
</dd></dl>

<div class="section" id="s-the-register-decorator">
<span id="the-register-decorator"></span><h3><code class="docutils literal notranslate"><span class="pre">register</span></code> 装饰器<a class="headerlink" href="#the-register-decorator" title="永久链接至标题">¶</a></h3>
<dl class="function">
<dt id="django.contrib.admin.register">
<code class="descname">register</code>(<em>*models</em>, <em>site=django.contrib.admin.sites.site</em>)<a class="headerlink" href="#django.contrib.admin.register" title="永久链接至目标">¶</a></dt>
<dd><p>还有一个装饰器用于注册你的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 类：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">.models</span> <span class="kn">import</span> <span class="n">Author</span>

<span class="nd">@admin</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>它接受一个或多个模型类，让它在 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 注册。如果你使用的是自定义的 <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a>，请使用 <code class="docutils literal notranslate"><span class="pre">site</span></code> 关键字参数传递它：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">.models</span> <span class="kn">import</span> <span class="n">Author</span><span class="p">,</span> <span class="n">Editor</span><span class="p">,</span> <span class="n">Reader</span>
<span class="kn">from</span> <span class="nn">myproject.admin_site</span> <span class="kn">import</span> <span class="n">custom_admin_site</span>

<span class="nd">@admin</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">Reader</span><span class="p">,</span> <span class="n">Editor</span><span class="p">,</span> <span class="n">site</span><span class="o">=</span><span class="n">custom_admin_site</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>如果你必须在你的模型管理类的 <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> 方法中引用你的模型管理类，例如 <code class="docutils literal notranslate"><span class="pre">super(PersonAdmin,</span> <span class="pre">self).__init__(*args,</span> <span class="pre">**kwargs)</span></code>，你就不能使用这个装饰器。你可以使用 <code class="docutils literal notranslate"><span class="pre">super().__init__(*args,</span> <span class="pre">**kwargs)</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-discovery-of-admin-files">
<span id="discovery-of-admin-files"></span><h3>发现管理文件<a class="headerlink" href="#discovery-of-admin-files" title="永久链接至标题">¶</a></h3>
<p>当你在 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中放入 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code> 时，Django 会自动在每个应用程序中寻找 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 模块并导入它。</p>
<dl class="class">
<dt id="django.contrib.admin.apps.AdminConfig">
<em class="property">class </em><code class="descclassname">apps.</code><code class="descname">AdminConfig</code><a class="headerlink" href="#django.contrib.admin.apps.AdminConfig" title="永久链接至目标">¶</a></dt>
<dd><p>这是管理员默认的 <a class="reference internal" href="../../applications.html#django.apps.AppConfig" title="django.apps.AppConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AppConfig</span></code></a> 类。当 Django 启动时，它会调用 <a class="reference internal" href="#django.contrib.admin.autodiscover" title="django.contrib.admin.autodiscover"><code class="xref py py-func docutils literal notranslate"><span class="pre">autodiscover()</span></code></a>。</p>
</dd></dl>

<dl class="class">
<dt id="django.contrib.admin.apps.SimpleAdminConfig">
<em class="property">class </em><code class="descclassname">apps.</code><code class="descname">SimpleAdminConfig</code><a class="headerlink" href="#django.contrib.admin.apps.SimpleAdminConfig" title="永久链接至目标">¶</a></dt>
<dd><p>这个类的工作原理和 <a class="reference internal" href="#django.contrib.admin.apps.AdminConfig" title="django.contrib.admin.apps.AdminConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminConfig</span></code></a> 一样，只是没有调用 <a class="reference internal" href="#django.contrib.admin.autodiscover" title="django.contrib.admin.autodiscover"><code class="xref py py-func docutils literal notranslate"><span class="pre">autodiscover()</span></code></a>。</p>
<dl class="attribute">
<dt id="django.contrib.admin.apps.SimpleAdminConfig.default_site">
<code class="descname">default_site</code><a class="headerlink" href="#django.contrib.admin.apps.SimpleAdminConfig.default_site" title="永久链接至目标">¶</a></dt>
<dd><p>一个点分隔的导入路径，用于导入默认的管理站点类，或者导入一个返回站点实例的可调用类。默认为 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin.sites.AdminSite'</span></code>。使用方法参见 <a class="reference internal" href="#overriding-default-admin-site"><span class="std std-ref">覆盖默认的管理站点</span></a>。</p>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="django.contrib.admin.autodiscover">
<code class="descname">autodiscover</code>()<a class="headerlink" href="#django.contrib.admin.autodiscover" title="永久链接至目标">¶</a></dt>
<dd><p>该函数试图在每个已安装的应用程序中导入一个 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 模块。这些模块将向管理注册模型。</p>
<p>通常情况下，你不需要直接调用这个函数，因为 <a class="reference internal" href="#django.contrib.admin.apps.AdminConfig" title="django.contrib.admin.apps.AdminConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminConfig</span></code></a> 会在 Django 启动时调用它。</p>
</dd></dl>

<p>如果你使用的是自定义的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>，通常会将所有的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 子类导入到你的代码中，并将它们注册到自定义的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code>。在这种情况下，为了禁止自动发现，你应该在你的 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中加入 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin.apps.SimpleAdminConfig'</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code>。</p>
</div>
<div class="section" id="s-modeladmin-options">
<span id="modeladmin-options"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 选项<a class="headerlink" href="#modeladmin-options" title="永久链接至标题">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 非常灵活。它有几个选项用于处理自定义界面。所有选项都在 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 子类中定义：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;pub_date&#39;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions" title="永久链接至目标">¶</a></dt>
<dd><p>要在变更列表页上提供的动作列表。详见 <a class="reference internal" href="actions.html"><span class="doc">管理动作</span></a>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_on_top">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_on_top</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_on_top" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_on_bottom">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_on_bottom</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_on_bottom" title="永久链接至目标">¶</a></dt>
<dd><p>控制动作栏在页面的哪个位置出现。默认情况下，管理员更改列表在页面顶部显示动作（<code class="docutils literal notranslate"><span class="pre">actions_on_top</span> <span class="pre">=</span> <span class="pre">True;</span> <span class="pre">actions_on_bottom</span> <span class="pre">=</span> <span class="pre">False</span></code>）。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.actions_selection_counter">
<code class="descclassname">ModelAdmin.</code><code class="descname">actions_selection_counter</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.actions_selection_counter" title="永久链接至目标">¶</a></dt>
<dd><p>控制是否在动作下拉菜单旁边显示选择计数器。默认情况下，管理员变更列表会显示它（<code class="docutils literal notranslate"><span class="pre">actions_selection_counter</span> <span class="pre">=</span> <span class="pre">True</span></code>）。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.date_hierarchy">
<code class="descclassname">ModelAdmin.</code><code class="descname">date_hierarchy</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.date_hierarchy" title="永久链接至目标">¶</a></dt>
<dd><p>将 <code class="docutils literal notranslate"><span class="pre">date_hierarchy</span></code> 设置为你的模型中 <code class="docutils literal notranslate"><span class="pre">DateField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code> 的名称，变化列表页将包括一个基于日期的向下扩展。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;pub_date&#39;</span>
</pre></div>
</div>
<p>你也可以使用 <code class="docutils literal notranslate"><span class="pre">__</span></code> 查找来指定相关模型上的字段，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">date_hierarchy</span> <span class="o">=</span> <span class="s1">&#39;author__pub_date&#39;</span>
</pre></div>
</div>
<p>这将根据可用的数据智能地填充自己，例如，如果所有的日期都在一个月内，它将只显示日级的向下扩展。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">date_hierarchy</span></code> 内部使用 <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.datetimes" title="django.db.models.query.QuerySet.datetimes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.datetimes()</span></code></a>。当启用时区支持时，请参考它的文档中的一些注意事项（<a class="reference internal" href="../../settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span> <span class="pre">=</span> <span class="pre">True</span></code></a>）。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.empty_value_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">empty_value_display</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="永久链接至目标">¶</a></dt>
<dd><p>该属性覆盖记录字段为空（<code class="docutils literal notranslate"><span class="pre">None</span></code>、空字符串等）的默认显示值。默认值是 <code class="docutils literal notranslate"><span class="pre">-</span></code> （破折号）。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;-empty-&#39;</span>
</pre></div>
</div>
<p>你也可以用 <a class="reference internal" href="#django.contrib.admin.AdminSite.empty_value_display" title="django.contrib.admin.AdminSite.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.empty_value_display</span></code></a> 覆盖所有管理页面的 <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code>，或者像这样覆盖特定字段：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;view_birth_date&#39;</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">empty_value</span><span class="o">=</span><span class="s1">&#39;???&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">view_birth_date</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">birth_date</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p><a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">empty_value</span></code> 参数相当于在以前的版本中直接在显示函数上设置 <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> 属性。为了向后兼容，仍然支持直接设置该属性。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.exclude">
<code class="descclassname">ModelAdmin.</code><code class="descname">exclude</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.exclude" title="永久链接至目标">¶</a></dt>
<dd><p>如果给定了这个属性，那么这个属性应该是一个要从表单中排除的字段名列表。</p>
<p>例如，我们考虑以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
    <span class="n">birth_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">(</span><span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你想让 <code class="docutils literal notranslate"><span class="pre">Author</span></code> 模型的表格只包括 <code class="docutils literal notranslate"><span class="pre">name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">title</span></code> 字段，你可以指定 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 或 <code class="docutils literal notranslate"><span class="pre">exclude</span></code> ，如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;birth_date&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>由于 Author 模型只有三个字段，即 <code class="docutils literal notranslate"><span class="pre">name</span></code>、<code class="docutils literal notranslate"><span class="pre">title</span></code> 和 <code class="docutils literal notranslate"><span class="pre">birth_date</span></code>，因此上述声明所产生的表单将包含完全相同的字段。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.fields" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 选项在 “添加” 和 “更改” 页面的表单中进行简单的布局修改，比如只显示可用字段的子集，修改它们的顺序，或者将它们分成几行。例如，你可以为 <a class="reference internal" href="../flatpages.html#django.contrib.flatpages.models.FlatPage" title="django.contrib.flatpages.models.FlatPage"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.flatpages.models.FlatPage</span></code></a> 模型定义一个更简单的管理表单版本，如下所示：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;content&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>在上面的例子中，只有 <code class="docutils literal notranslate"><span class="pre">url</span></code>、<code class="docutils literal notranslate"><span class="pre">title</span></code> 和 <code class="docutils literal notranslate"><span class="pre">content</span></code> 等字段会依次显示。<code class="docutils literal notranslate"><span class="pre">fields</span></code> 可以包含 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.readonly_fields</span></code></a> 中定义的值，以只读方式显示。</p>
<p>对于更复杂的布局需求，请参阅 <code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldets</span></code> 选项。</p>
<p><code class="docutils literal notranslate"><span class="pre">fields</span></code> 选项接受与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> 相同类型的值，只是不接受作为可调用对象的值。模型和模型管理方法的名称只有在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a> 中列出时才会被使用。</p>
<p>要在同一行显示多个字段，将这些字段包在自己的元组中。在这个例子中，<code class="docutils literal notranslate"><span class="pre">url</span></code> 和 <code class="docutils literal notranslate"><span class="pre">title</span></code> 字段将显示在同一行，<code class="docutils literal notranslate"><span class="pre">content</span></code> 字段将显示在它们下面的一行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fields</span> <span class="o">=</span> <span class="p">((</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">),</span> <span class="s1">&#39;content&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">注解</p>
<p class="last">这个 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 选项不应与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> 选项中的 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 字典键混淆，下一节将介绍。</p>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 或 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> 选项都不存在，Django 将默认在一个单一的字段集中显示每个非 <code class="docutils literal notranslate"><span class="pre">AutoField</span></code> 且有 <code class="docutils literal notranslate"><span class="pre">editable=True</span></code> 的字段，顺序与模型中定义的字段相同。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.fieldsets">
<code class="descclassname">ModelAdmin.</code><code class="descname">fieldsets</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.fieldsets" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> 来控制管理员 “添加” 和 “更改” 页面的布局。</p>
<p><code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> 是一个由两个元组组成的列表，其中每个元组代表管理表单页面上的一个 <code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code>。（一个 <code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code> 是表单的一个 “部分”。)</p>
<p>这两个元组的格式是 <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">field_options)</span></code>，其中 <code class="docutils literal notranslate"><span class="pre">name</span></code> 是代表字段集标题的字符串，<code class="docutils literal notranslate"><span class="pre">field_options</span></code> 是关于字段集的信息字典，包括要在其中显示的字段列表。</p>
<p>一个完整的例子，取自 <a class="reference internal" href="../flatpages.html#django.contrib.flatpages.models.FlatPage" title="django.contrib.flatpages.models.FlatPage"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.flatpages.models.FlatPage</span></code></a> 模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">FlatPageAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">fieldsets</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;content&#39;</span><span class="p">,</span> <span class="s1">&#39;sites&#39;</span><span class="p">)</span>
        <span class="p">}),</span>
        <span class="p">(</span><span class="s1">&#39;Advanced options&#39;</span><span class="p">,</span> <span class="p">{</span>
            <span class="s1">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;collapse&#39;</span><span class="p">,),</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;registration_required&#39;</span><span class="p">,</span> <span class="s1">&#39;template_name&#39;</span><span class="p">),</span>
        <span class="p">}),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>这样一来，管理页面就变成了这样：</p>
<img alt="../../../_images/fieldsets.png" src="../../../_images/fieldsets.png" />
<p>如果 <code class="docutils literal notranslate"><span class="pre">fieldsets</span></code> 或 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> 选项都不存在，Django 将默认在一个单一的表单集中显示每个非 <code class="docutils literal notranslate"><span class="pre">AutoField</span></code> 且有 <code class="docutils literal notranslate"><span class="pre">editable=True</span></code> 的字段，顺序与模型中定义的字段相同。</p>
<p><code class="docutils literal notranslate"><span class="pre">field_options</span></code> 字典可以有以下键：</p>
<ul>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">fields</span></code></dt>
<dd><p class="first">要在该字段集中显示的字段名元组。这个键是必需的。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;address&#39;</span><span class="p">,</span> <span class="s1">&#39;city&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p>与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> 选项一样，要在同一行显示多个字段，请将这些字段封装在自己的元组中。在这个例子中，<code class="docutils literal notranslate"><span class="pre">first_name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">last_name</span></code> 字段将显示在同一行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="p">((</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">),</span> <span class="s1">&#39;address&#39;</span><span class="p">,</span> <span class="s1">&#39;city&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">fields</span></code> 可以包含 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a> 中定义的值，以只读方式显示。</p>
<p class="last">如果你将可调用的名称添加到 <code class="docutils literal notranslate"><span class="pre">fields</span></code> 中，与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> 选项的规则相同：可调用的名称必须列在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a> 中。</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">classes</span></code></dt>
<dd><p class="first">一个包含额外 CSS 类的列表或元组，用于应用到字段集。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="s1">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;wide&#39;</span><span class="p">,</span> <span class="s1">&#39;extrapretty&#39;</span><span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<p class="last">默认的管理网站样式表定义的两个有用的类是 <code class="docutils literal notranslate"><span class="pre">collapse</span></code> 和 <code class="docutils literal notranslate"><span class="pre">wide</span></code>。<code class="docutils literal notranslate"><span class="pre">collapse</span></code> 风格的字段集将在管理中被初始折叠，并被一个小的 “点击展开” 链接所取代；<code class="docutils literal notranslate"><span class="pre">wide</span></code> 风格的字段集将被赋予额外的水平空间。<code class="docutils literal notranslate"><span class="pre">wide</span></code> 风格的字段集将获得额外的水平空间。</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><code class="docutils literal notranslate"><span class="pre">description</span></code></dt>
<dd><p class="first">一串可选的额外文本，显示在每个字段集的顶部，在字段集的标题下。这个字符串在 <a class="reference internal" href="#django.contrib.admin.TabularInline" title="django.contrib.admin.TabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">TabularInline</span></code></a> 中由于其布局的原因而无法显示。</p>
<p class="last">请注意，当这个值在管理界面显示时，它不是 <em>HTML 转义后的</em>。如果你愿意的话，这可以让你包含 HTML。或者你可以使用纯文本和 <a class="reference internal" href="../../utils.html#django.utils.html.escape" title="django.utils.html.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.utils.html.escape()</span></code></a> 来转义任何 HTML 特殊字符。</p>
</dd>
</dl>
</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.filter_horizontal">
<code class="descclassname">ModelAdmin.</code><code class="descname">filter_horizontal</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，在管理网站中显示一个 <a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">&lt;select</span> <span class="pre">multiple&gt;</span></code>。但是，多选框在选择很多项目时，会很难用。在这个列表中添加一个 <a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>，就可以改用一个不显眼的 JavaScript “过滤器” 界面，在选项中进行搜索。未选择和选择的选项并排出现在两个框中。参见 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="django.contrib.admin.ModelAdmin.filter_vertical"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_vertical</span></code></a> 来使用垂直界面。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.filter_vertical">
<code class="descclassname">ModelAdmin.</code><code class="descname">filter_vertical</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="永久链接至目标">¶</a></dt>
<dd><p>与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="django.contrib.admin.ModelAdmin.filter_horizontal"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_horizontal</span></code></a> 相同，但使用垂直显示过滤界面，未选择的选项框出现在选择选项框的上方。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.form">
<code class="descclassname">ModelAdmin.</code><code class="descname">form</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.form" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，会为你的模型动态创建一个 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code>。它用于创建在添加／更改页面上显示的表单。你可以很容易地提供你自己的 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 来覆盖添加／更改页面上的任何默认表单行为。另外，你可以使用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_form" title="django.contrib.admin.ModelAdmin.get_form"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_form()</span></code></a> 方法来定制默认的表单，而不是指定一个全新的表单。</p>
<p>示例请参见 <a class="reference internal" href="#admin-custom-validation"><span class="std std-ref">在管理中添加自定义验证</span></a> 一节。</p>
<div class="admonition-note admonition">
<p class="first admonition-title">注解</p>
<p>如果你在 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 上定义了 <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> 属性，你也必须定义 <code class="docutils literal notranslate"><span class="pre">Meta.fields</span></code> 属性（或 <code class="docutils literal notranslate"><span class="pre">Meta.exclude</span></code> 属性）。但是，由于管理有自己定义字段的方式，所以 <code class="docutils literal notranslate"><span class="pre">Meta.field</span></code> 属性将被忽略。</p>
<p class="last">如果 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 只用于管理，最简单的解决办法是省略 <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> 属性，因为 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 将提供正确的模型。或者，你可以在 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 类中设置 <code class="docutils literal notranslate"><span class="pre">fields</span> <span class="pre">=</span> <span class="pre">[]</span></code> 以满足 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 的验证。</p>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">注解</p>
<p>如果你的 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 和 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 都定义了 <code class="docutils literal notranslate"><span class="pre">exclude</span></code> 选项，那么 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 优先：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Person</span>

<span class="k">class</span> <span class="nc">PersonForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">Person</span>
        <span class="n">exclude</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;age&#39;</span><span class="p">]</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">PersonForm</span>
</pre></div>
</div>
<p class="last">在上面的例子中，“年龄” 字段将被排除，但 “姓名” 字段将被包含在生成的表单中。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.formfield_overrides">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_overrides</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_overrides" title="永久链接至目标">¶</a></dt>
<dd><p>这提供了一个快速而简单的方法来覆盖一些 <a class="reference internal" href="../../forms/fields.html#django.forms.Field" title="django.forms.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 选项，以便在管理中使用。<code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> 是一个字典，它将字段类映射成一个参数的字典，以便在构造时传递给字段。</p>
<p>由于这有点抽象，我们来看一个具体的例子。<code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> 最常见的用法是为某一类型的字段添加一个自定义部件。所以，想象一下，我们写了一个 <code class="docutils literal notranslate"><span class="pre">RichTextEditorWidget</span></code>，我们想用于大文本字段，而不是默认的 <code class="docutils literal notranslate"><span class="pre">&lt;textarea&gt;</span></code>。下面是我们要做的：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="c1"># Import our custom widget and our model from where they&#39;re defined</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">MyModel</span>
<span class="kn">from</span> <span class="nn">myapp.widgets</span> <span class="kn">import</span> <span class="n">RichTextEditorWidget</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">formfield_overrides</span> <span class="o">=</span> <span class="p">{</span>
        <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;widget&#39;</span><span class="p">:</span> <span class="n">RichTextEditorWidget</span><span class="p">},</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>注意，字典中的键是实际的字段类，<em>不是</em> 字符串。值是另一个字典；这些参数将被传递给表单字段的 <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> 方法。详见 <a class="reference internal" href="../../forms/api.html"><span class="doc">表单 API</span></a>。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>如果你想使用一个带有关系字段的自定义部件（例如 <a class="reference internal" href="../../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">ForeignKey</span></code></a> 或 <a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>），确保你没有在 <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code>、<code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> 或 <code class="docutils literal notranslate"><span class="pre">autocomplete_fields</span></code> 中包含该字段的名称。</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">formfield_overrides</span></code> 不会让你改变有 <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code>、<code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> 或 <code class="docutils literal notranslate"><span class="pre">autocomplete_fields</span></code> 设置的关系字段上的部件。这是因为 <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code>、<code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> 和 <code class="docutils literal notranslate"><span class="pre">autocomplete_fields</span></code> 意味着自己的自定义部件。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.inlines">
<code class="descclassname">ModelAdmin.</code><code class="descname">inlines</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.inlines" title="永久链接至目标">¶</a></dt>
<dd><p>参见下面的 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a> 对象以及 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="django.contrib.admin.ModelAdmin.get_formsets_with_inlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_formsets_with_inlines()</span></code></a>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_display</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_display" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 来控制哪些字段显示在管理的变更列表页面。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你不设置 <code class="docutils literal notranslate"><span class="pre">list_display</span></code>，管理网站将显示一个单列，显示每个对象的 <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> 表示。</p>
<p>有四种类型的值可以在 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中使用。除了最简单的可以使用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器用于自定义字段的显示方式：</p>
<ul>
<li><p class="first">模型字段的名称。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">一个接受一个参数的可调用对象，即模型实例。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Name&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">upper_case_name</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">last_name</span><span class="p">))</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="n">upper_case_name</span><span class="p">,)</span>
</pre></div>
</div>
</li>
<li><p class="first">表示 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 方法的字符串，该方法接受一个参数，即模型实例。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;upper_case_name&#39;</span><span class="p">,)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Name&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">upper_case_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">last_name</span><span class="p">))</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
</pre></div>
</div>
</li>
<li><p class="first">代表模型属性或方法的字符串（没有任何必要的参数）。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">birthday</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Birth decade&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">decade_born_in</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%d</span><span class="s1">’s&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">birthday</span><span class="o">.</span><span class="n">year</span> <span class="o">//</span> <span class="mi">10</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;decade_born_in&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>关于 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 要注意的几个特殊情况：</p>
<ul>
<li><p class="first">如果字段是 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>，Django 会显示相关对象的 <code class="docutils literal notranslate"><span class="pre">__str__()</span></code>。</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 字段不被支持，因为这需要为表中的每一行单独执行一条 SQL 语句。如果你还是想这样做，请给你的模型一个自定义方法，并将该方法的名称添加到 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中。（更多关于 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中的自定义方法，请参见下文）。</p>
</li>
<li><p class="first">如果字段是 <code class="docutils literal notranslate"><span class="pre">BooleanField</span></code>，Django 会显示一个漂亮的 “是”、“否” 或 “未知” 图标，而不是 <code class="docutils literal notranslate"><span class="pre">True</span></code>、<code class="docutils literal notranslate"><span class="pre">False</span></code> 或 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</li>
<li><p class="first">如果给定的字符串是模型的一个方法，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 或者是一个可调用的方法，Django 默认会对输出进行 HTML 转义。如果要转义用户的输入，并允许你自己使用未转义的标签，可以使用 <a class="reference internal" href="../../utils.html#django.utils.html.format_html" title="django.utils.html.format_html"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_html()</span></code></a>。</p>
<p>下面是一个完整的示例模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="kn">import</span> <span class="n">format_html</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">color_code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span>
    <span class="k">def</span> <span class="nf">colored_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span>
            <span class="s1">&#39;&lt;span style=&quot;color: #</span><span class="si">{}</span><span class="s1">;&quot;&gt;</span><span class="si">{}</span><span class="s1"> </span><span class="si">{}</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">color_code</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span><span class="p">,</span>
        <span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;colored_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">正如一些例子已经证明的那样，当使用一个可调用对象、一个模型方法或一个 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 方法时，你可以通过用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器包装可调用对象，并传递 <code class="docutils literal notranslate"><span class="pre">description</span></code> 参数来自定义列的标题。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p><a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">description</span></code> 参数相当于在以前的版本中直接在显示函数上设置 <code class="docutils literal notranslate"><span class="pre">short_description</span></code> 属性。为了向后兼容，仍然支持直接设置该属性。</p>
</div>
</li>
<li><p class="first">如果一个字段的值是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，一个空字符串，或者一个没有元素的可迭代字段，Django 将显示 <code class="docutils literal notranslate"><span class="pre">-</span></code> （破折号）。你可以用 <a class="reference internal" href="#django.contrib.admin.AdminSite.empty_value_display" title="django.contrib.admin.AdminSite.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.empty_value_display</span></code></a> 来覆盖这一点：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;(None)&#39;</span>
</pre></div>
</div>
<p>你也可以使用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="django.contrib.admin.ModelAdmin.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.empty_value_display</span></code></a> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">empty_value_display</span> <span class="o">=</span> <span class="s1">&#39;unknown&#39;</span>
</pre></div>
</div>
<p>或在字段级别：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;birth_date_view&#39;</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">empty_value</span><span class="o">=</span><span class="s1">&#39;unknown&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">birth_date_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
         <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">birth_date</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p><a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">empty_value</span></code> 参数相当于在以前的版本中直接在显示函数上设置 <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> 属性。为了向后兼容，仍然支持直接设置该属性。</p>
</div>
</li>
<li><p class="first">如果给定的字符串是模型的一个方法，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 或一个返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>、<code class="docutils literal notranslate"><span class="pre">False</span></code> 或 <code class="docutils literal notranslate"><span class="pre">None</span></code> 的可调用对象，如果你用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器包装该方法，传递 <code class="docutils literal notranslate"><span class="pre">boolean</span></code> 参数，并将其值设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">birthday</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">boolean</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">born_in_fifties</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">1950</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">birthday</span><span class="o">.</span><span class="n">year</span> <span class="o">&lt;</span> <span class="mi">1960</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;born_in_fifties&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p><a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">boolean</span></code> 参数相当于在以前的版本中直接在显示函数上设置 <code class="docutils literal notranslate"><span class="pre">boolean</span></code> 属性。为了向后兼容，仍然支持直接设置该属性。</p>
</div>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">__str__()</span></code> 方法在 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中和其他模型方法一样有效，所以完全可以这样做：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;__str__&#39;</span><span class="p">,</span> <span class="s1">&#39;some_other_field&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">通常情况下，<code class="docutils literal notranslate"><span class="pre">list_display</span></code> 的元素如果不是实际的数据库字段，就不能用于排序（因为 Django 在数据库层面进行了所有的排序）。</p>
<p>但是，如果 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 的元素代表某个数据库字段，你可以通过在方法上使用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器，传递 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数来表明这个事实：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="kn">import</span> <span class="n">format_html</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">color_code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">ordering</span><span class="o">=</span><span class="s1">&#39;first_name&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">colored_first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span>
            <span class="s1">&#39;&lt;span style=&quot;color: #</span><span class="si">{}</span><span class="s1">;&quot;&gt;</span><span class="si">{}</span><span class="s1">&lt;/span&gt;&#39;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">color_code</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span>
        <span class="p">)</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;colored_first_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>上面会告诉 Django 在管理中按 <code class="docutils literal notranslate"><span class="pre">colored_first_name</span></code> 排序时，按 <code class="docutils literal notranslate"><span class="pre">first_name</span></code> 字段排序。</p>
<p>要用 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数表示降序，可以在字段名上使用连字符前缀。使用上面的例子，它看起来像：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">ordering</span><span class="o">=</span><span class="s1">&#39;-first_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数支持查询查找，在相关模型上按值排序。这个例子在列表显示中包含了一个 “作者的姓” 列，并允许按姓排序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Blog</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">BlogAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="s1">&#39;author_first_name&#39;</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">ordering</span><span class="o">=</span><span class="s1">&#39;author__first_name&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">author_first_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">author</span><span class="o">.</span><span class="n">first_name</span>
</pre></div>
</div>
<p><a class="reference internal" href="../../models/expressions.html"><span class="doc">查询表达式</span></a> 可与 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数一起使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Value</span>
<span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="kn">import</span> <span class="n">Concat</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">ordering</span><span class="o">=</span><span class="n">Concat</span><span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="n">Value</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">),</span> <span class="s1">&#39;last_name&#39;</span><span class="p">))</span>
    <span class="k">def</span> <span class="nf">full_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p><a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数相当于在以前的版本中直接在显示函数上设置 <code class="docutils literal notranslate"><span class="pre">admin_order_field</span></code> 属性。为了向后兼容，仍然支持直接设置属性。</p>
</div>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">list_display</span></code> 的元素也可以是属性：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span>
        <span class="n">ordering</span><span class="o">=</span><span class="s1">&#39;last_name&#39;</span><span class="p">,</span>
        <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Full name of the person&#39;</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">def</span> <span class="nf">full_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;full_name&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>注意 <code class="docutils literal notranslate"><span class="pre">&#64;property</span></code> 必须在 <code class="docutils literal notranslate"><span class="pre">&#64;display</span></code> 之上。如果你使用老方法 —— 直接设置与 display 相关的属性，而不是使用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器 —— 请注意，必须使用 <code class="docutils literal notranslate"><span class="pre">property()</span></code> 函数，而 <strong>不是</strong> <code class="docutils literal notranslate"><span class="pre">&#64;property</span></code> 装饰器：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_property</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">first_name</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">last_name</span>
<span class="n">my_property</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Full name of the person&quot;</span>
<span class="n">my_property</span><span class="o">.</span><span class="n">admin_order_field</span> <span class="o">=</span> <span class="s1">&#39;last_name&#39;</span>

<span class="n">full_name</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">my_property</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中的字段名也会以 CSS 类的形式出现在 HTML 输出中，在每个 <code class="docutils literal notranslate"><span class="pre">&lt;th&gt;</span></code> 元素上以 <code class="docutils literal notranslate"><span class="pre">column-&lt;field_name&gt;</span></code> 的形式出现。例如，这可以用来在 CSS 文件中设置列宽。</p>
</li>
<li><p class="first">Django 会尝试按照这个顺序解释 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 的每个元素：</p>
<ul class="simple">
<li>一个模型的字段。</li>
<li>一个可调用对象。</li>
<li>一个代表 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 属性的字符串。</li>
<li>一个代表模型属性的字符串。</li>
</ul>
<p>例如，如果你有 <code class="docutils literal notranslate"><span class="pre">first_name</span></code> 作为一个模型字段和 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 属性，将使用模型字段。</p>
</li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_display_links">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_display_links</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_display_links" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> 来控制 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> 中的字段是否以及哪些字段应该被链接到对象的 “更改” 页面。</p>
<p>默认情况下，更改列表页将把第一列 —— <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中指定的第一个字段 —— 链接到每个项目的更改页面。但是 <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> 让你改变这一点：</p>
<ul>
<li><p class="first">将其设置为 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则完全没有链接。</p>
</li>
<li><p class="first">将它设置为一个列表或元组字段（格式与 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 相同），你希望将其列转换为链接。</p>
<p>你可以指定一个或多个字段。只要字段出现在 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中，Django 就不会关心链接了多少（或多少）字段。唯一的要求是，如果你想以这种方式使用 <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code>，你必须定义 <code class="docutils literal notranslate"><span class="pre">list_display</span></code>。</p>
</li>
</ul>
<p>在这个例子中，<code class="docutils literal notranslate"><span class="pre">first_name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">last_name</span></code> 字段将在更改列表页面上被链接：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;birthday&#39;</span><span class="p">)</span>
    <span class="n">list_display_links</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>在这个例子中，更改列表页网格将没有链接：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AuditEntryAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;timestamp&#39;</span><span class="p">,</span> <span class="s1">&#39;message&#39;</span><span class="p">)</span>
    <span class="n">list_display_links</span> <span class="o">=</span> <span class="kc">None</span>
</pre></div>
</div>
</dd></dl>

<span class="target" id="admin-list-editable"></span><dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_editable">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_editable</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_editable" title="永久链接至目标">¶</a></dt>
<dd><p>将 <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> 设置为模型上允许在更改列表页上编辑的字段名称列表。也就是说，在 <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> 中列出的字段将作为表单部件显示在变更列表页上，允许用户一次编辑和保存多行。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p><code class="docutils literal notranslate"><span class="pre">list_editable</span></code> 以特定方式与其他几个选项进行交互；你应该注意以下规则：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">list_editable</span></code> 中的任何字段都必须在 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> 中。你不能编辑一个没有显示的字段！</li>
<li>同一字段不能同时列在 <code class="docutils literal notranslate"><span class="pre">list_editable</span></code> 和 <code class="docutils literal notranslate"><span class="pre">list_display_links</span></code> 中 —— 一个字段不能既是表单又是链接。</li>
</ul>
<p class="last">如果这些规则中的任何一条被破坏，你会得到一个验证错误。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_filter">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_filter</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_filter" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">list_filter</span></code> 来激活管理更改列表页面右侧侧栏的过滤器，如下截图所示：</p>
<img alt="../../../_images/list_filter.png" src="../../../_images/list_filter.png" />
<p><code class="docutils literal notranslate"><span class="pre">list_filter</span></code> 应是一个元素的列表或元组，其中每个元素应是下列类型之一：</p>
<ul>
<li><p class="first">一个字段名，其中指定的字段应该是 <code class="docutils literal notranslate"><span class="pre">BooleanField`</span></code>、<cite>CharField`</cite>、<code class="docutils literal notranslate"><span class="pre">DateField</span></code>、<code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>、<code class="docutils literal notranslate"><span class="pre">IntegerField</span></code>、<code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;is_staff&#39;</span><span class="p">,</span> <span class="s1">&#39;company&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">list_filter</span></code> 中的字段名也可以使用 <code class="docutils literal notranslate"><span class="pre">__</span></code> 查找来跨越关系，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">UserAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;company__name&#39;</span><span class="p">,)</span>
</pre></div>
</div>
</li>
<li><p class="first">继承自 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.SimpleListFilter</span></code> 的类，你需要为其提供 <code class="docutils literal notranslate"><span class="pre">title</span></code> 和 <code class="docutils literal notranslate"><span class="pre">parameter_name</span></code> 属性，并覆盖 <code class="docutils literal notranslate"><span class="pre">lookups</span></code> 和 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 方法，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>

<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">gettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="k">class</span> <span class="nc">DecadeBornListFilter</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">SimpleListFilter</span><span class="p">):</span>
    <span class="c1"># Human-readable title which will be displayed in the</span>
    <span class="c1"># right admin sidebar just above the filter options.</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;decade born&#39;</span><span class="p">)</span>

    <span class="c1"># Parameter for the filter that will be used in the URL query.</span>
    <span class="n">parameter_name</span> <span class="o">=</span> <span class="s1">&#39;decade&#39;</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a list of tuples. The first element in each</span>
<span class="sd">        tuple is the coded value for the option that will</span>
<span class="sd">        appear in the URL query. The second element is the</span>
<span class="sd">        human-readable name for the option that will appear</span>
<span class="sd">        in the right sidebar.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="p">(</span><span class="s1">&#39;80s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the eighties&#39;</span><span class="p">)),</span>
            <span class="p">(</span><span class="s1">&#39;90s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the nineties&#39;</span><span class="p">)),</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the filtered queryset based on the value</span>
<span class="sd">        provided in the query string and retrievable via</span>
<span class="sd">        `self.value()`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Compare the requested value (either &#39;80s&#39; or &#39;90s&#39;)</span>
        <span class="c1"># to decide how to filter the queryset.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;80s&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1980</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                                    <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1989</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;90s&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">queryset</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                                    <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1999</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">,)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>为方便起见，<code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 对象被传递给 <code class="docutils literal notranslate"><span class="pre">lookups</span></code> 和 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 方法，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AuthDecadeBornListFilter</span><span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">lookups</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">queryset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">)</span>
</pre></div>
</div>
<p>另外，为了方便起见，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 对象被传递给 <code class="docutils literal notranslate"><span class="pre">lookups</span></code> 方法，例如，如果你想根据现有数据进行查找：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AdvancedDecadeBornListFilter</span><span class="p">(</span><span class="n">DecadeBornListFilter</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">lookups</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">model_admin</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Only show the lookups if there actually is</span>
<span class="sd">        anyone born in the corresponding decades.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">model_admin</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1980</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                      <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1989</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s1">&#39;80s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the eighties&#39;</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">birthday__gte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
                      <span class="n">birthday__lte</span><span class="o">=</span><span class="n">date</span><span class="p">(</span><span class="mi">1999</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">31</span><span class="p">))</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">yield</span> <span class="p">(</span><span class="s1">&#39;90s&#39;</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;in the nineties&#39;</span><span class="p">))</span>
</pre></div>
</div>
</div>
</li>
<li><p class="first">一个元组，其中第一个元素是字段名，第二个元素是继承自 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.FieldListFilter</span></code> 的类，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;is_staff&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">BooleanFieldListFilter</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>你可以使用 <code class="docutils literal notranslate"><span class="pre">RelatedOnlyFieldListFilter</span></code> 将相关模型的选择限制在该关系所涉及的对象上：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BookAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">RelatedOnlyFieldListFilter</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>假设 <code class="docutils literal notranslate"><span class="pre">author</span></code> 是 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 到 <code class="docutils literal notranslate"><span class="pre">User</span></code> 模型，这将会把 <code class="docutils literal notranslate"><span class="pre">list_filter</span></code> 的选择限制在写过书的用户，而不是列出所有用户。</p>
<p>你可以使用 <code class="docutils literal notranslate"><span class="pre">EmptyFieldListFilter</span></code> 来过滤空值，它既可以过滤空字符串也可以过滤空值，这取决于字段允许存储的内容：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BookAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">EmptyFieldListFilter</span><span class="p">),</span>
    <span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">FieldListFilter</span></code> 的 API 被认为是内部的，可能会被改变。</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">不支持 <code class="xref py py-class docutils literal notranslate"><span class="pre">GenericForeignKey</span></code> 字段。</p>
</div>
<div class="versionadded">
<span class="title">New in Django 3.1:</span> <p>增加了 <code class="docutils literal notranslate"><span class="pre">EmptyFieldListFilter</span></code> 类。</p>
</div>
</li>
</ul>
<p>列表过滤器通常只有在过滤器有多个选择时才会出现。过滤器的 <code class="docutils literal notranslate"><span class="pre">has_output()</span></code> 方法控制它是否出现。</p>
<p>可以指定一个自定义模板来呈现列表过滤器：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">FilterWithCustomTemplate</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">SimpleListFilter</span><span class="p">):</span>
    <span class="n">template</span> <span class="o">=</span> <span class="s2">&quot;custom_template.html&quot;</span>
</pre></div>
</div>
<p>具体的例子请看 Django 提供的默认模板（<code class="docutils literal notranslate"><span class="pre">admin/filter.html</span></code>）。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_max_show_all">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_max_show_all</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_max_show_all" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">list_max_show_all</span></code> 来控制 “全部显示” 的管理员更改列表页面上可以出现多少个项目。只有当总结果数小于或等于此配置时，管理才会在更改列表中显示 “全部显示” 链接。默认情况下，这个配置为 <code class="docutils literal notranslate"><span class="pre">200</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_per_page">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_per_page</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_per_page" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">list_per_page</span></code> 来控制每个分页的管理变更列表页面上出现多少个项目。默认情况下，设置为 <code class="docutils literal notranslate"><span class="pre">100</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.list_select_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">list_select_related</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.list_select_related" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">list_select_related</span></code> 告诉 Django 在检索管理变更列表页的对象列表时使用 <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.select_related" title="django.db.models.query.QuerySet.select_related"><code class="xref py py-meth docutils literal notranslate"><span class="pre">select_related()</span></code></a>。这样可以省去一堆数据库查询。</p>
<p>该值应是布尔值、列表或元组。默认值是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>当值为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时，<code class="docutils literal notranslate"><span class="pre">select_related()</span></code> 总是会被调用。当值设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 时，Django 将查看 <code class="docutils literal notranslate"><span class="pre">list_display</span></code>，如果有 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>，则调用 <code class="docutils literal notranslate"><span class="pre">select_related()</span></code>。</p>
<p>如果你需要更精细的控制，可以使用元组（或列表）作为 <code class="docutils literal notranslate"><span class="pre">list_select_related</span></code> 的值。空元组将阻止 Django 调用 <code class="docutils literal notranslate"><span class="pre">select_related</span></code>。任何其他元组将直接传递给 <code class="docutils literal notranslate"><span class="pre">select_related</span></code> 作为参数。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_select_related</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">,</span> <span class="s1">&#39;category&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>将调用 <code class="docutils literal notranslate"><span class="pre">select_related('author',</span> <span class="pre">'category')</span></code>。</p>
<p>如果需要根据请求指定一个动态值，可以实现一个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_select_related" title="django.contrib.admin.ModelAdmin.get_list_select_related"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_list_select_related()</span></code></a> 方法。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">当 <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.select_related" title="django.db.models.query.QuerySet.select_related"><code class="xref py py-meth docutils literal notranslate"><span class="pre">select_related()</span></code></a> 已经在变更列表的 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 上被调用时，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 会忽略这个属性。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.ordering">
<code class="descclassname">ModelAdmin.</code><code class="descname">ordering</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.ordering" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 来指定对象列表在 Django 管理视图中的排序方式。这应该是一个列表或元组，格式与模型的 <a class="reference internal" href="../../models/options.html#django.db.models.Options.ordering" title="django.db.models.Options.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a> 参数相同。</p>
<p>如果没有提供，Django 管理员将使用模型的默认排序。</p>
<p>如果你需要指定一个动态的顺序（例如取决于用户或语言），你可以实现一个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_ordering" title="django.contrib.admin.ModelAdmin.get_ordering"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_ordering()</span></code></a> 方法。</p>
<div class="admonition-performance-considerations-with-ordering-and-sorting admonition">
<p class="first admonition-title">排序和分类的性能考虑</p>
<p>为了确保结果的确定性排序，如果不能找到一个单一的或唯一的字段集，提供总的排序，那么变更表就会在排序中增加 <code class="docutils literal notranslate"><span class="pre">pk</span></code>。</p>
<p class="last">例如，如果默认的排序是按非唯一的 <code class="docutils literal notranslate"><span class="pre">name</span></code> 字段排序，那么更改列表就按 <code class="docutils literal notranslate"><span class="pre">name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">pk</span></code> 排序。如果你有很多行，而且在 <code class="docutils literal notranslate"><span class="pre">name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">pk</span></code> 上没有索引，这可能会表现得很差。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.paginator">
<code class="descclassname">ModelAdmin.</code><code class="descname">paginator</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.paginator" title="永久链接至目标">¶</a></dt>
<dd><p>用于分页的分页器类。默认情况下，使用 <a class="reference internal" href="../../paginator.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.core.paginator.Paginator</span></code></a>。如果自定义的分页器类没有和 <a class="reference internal" href="../../paginator.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.core.paginator.Paginator</span></code></a> 一样的构造函数接口，你还需要为 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_paginator" title="django.contrib.admin.ModelAdmin.get_paginator"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_paginator()</span></code></a> 提供一个实现。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.prepopulated_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">prepopulated_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="永久链接至目标">¶</a></dt>
<dd><p>将 <code class="docutils literal notranslate"><span class="pre">prepopulated_fields</span></code> 设置为一个字典，将字段名称映射到它应该预先填充的字段：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">prepopulated_fields</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;slug&quot;</span><span class="p">:</span> <span class="p">(</span><span class="s2">&quot;title&quot;</span><span class="p">,)}</span>
</pre></div>
</div>
<p>当设置时，给定的字段将使用一点 JavaScript 从分配的字段中填充。这个功能的主要用途是从一个或多个其他字段自动生成 <code class="docutils literal notranslate"><span class="pre">SlugField</span></code> 字段的值。生成的值是通过连接源字段的值，然后将结果转化为有效的 slug（例如，用破折号代替空格，使用 ASCII 字母的小写字母）。</p>
<p>预填充的字段在保存值后不会被 JavaScript 修改。通常情况下，不希望 slug 发生变化（如果对象中使用了 slug，会导致对象的 URL 发生变化）。</p>
<p><code class="docutils literal notranslate"><span class="pre">prepopulated_fields</span></code> 不接受 <code class="docutils literal notranslate"><span class="pre">DateTimeField</span></code>、<code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code>、<code class="docutils literal notranslate"><span class="pre">OneToOneField</span></code> 和 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 字段。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>在旧版本中，各种英文停顿词会从生成值中删除。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.preserve_filters">
<code class="descclassname">ModelAdmin.</code><code class="descname">preserve_filters</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.preserve_filters" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，在创建、编辑或删除对象后，应用的过滤器会被保存在列表视图中。您可以通过将此属性设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 来清除过滤器。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.radio_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">radio_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.radio_fields" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，Django 的管理对于 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或设置了 <code class="docutils literal notranslate"><span class="pre">choices</span></code> 的字段使用选择框界面（&lt;select&gt;）。如果字段存在于 <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> 中，Django 将使用单选按钮接口代替。假设 <code class="docutils literal notranslate"><span class="pre">group</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Person</span></code> 模型上的一个 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">radio_fields</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;group&quot;</span><span class="p">:</span> <span class="n">admin</span><span class="o">.</span><span class="n">VERTICAL</span><span class="p">}</span>
</pre></div>
</div>
<p>你可以在 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin</span></code> 模块中选择使用 <code class="docutils literal notranslate"><span class="pre">HORIZONTAL</span></code> 或 <code class="docutils literal notranslate"><span class="pre">VERTICAL</span></code>。</p>
<p>不要在 <code class="docutils literal notranslate"><span class="pre">radio_fields</span></code> 中包含一个字段，除非它是 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或已设置 <code class="docutils literal notranslate"><span class="pre">choices</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.autocomplete_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">autocomplete_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.autocomplete_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">autocomplete_fields</span></code> 是一个 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 和／或 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 字段的列表，你想将其改为 <a class="reference external" href="https://select2.org/">Select2</a> 自动完成输入。</p>
<p><code class="docutils literal notranslate"><span class="pre">autocomplete_fields</span></code> 是一个 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 和／或的列表，默认情况下，管理对这些字段使用选择框接口（<code class="docutils literal notranslate"><span class="pre">&lt;select&gt;</span></code>）。有时你不想产生选择所有相关实例在下拉中显示的开销。<code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 字段你想改成 <a class="reference external" href="https://select2.org/">Select2</a> 自动完成输入。</p>
<p>Select2 输入看起来与默认输入类似，但自带搜索功能，异步加载选项。如果相关模型有很多实例，这样会更快、更方便用户使用。</p>
<p>你必须在相关对象的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上定义 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.search_fields" title="django.contrib.admin.ModelAdmin.search_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">search_fields</span></code></a>，因为自动完成搜索使用它。</p>
<p>为了避免未经授权的数据泄露，用户必须拥有相关对象的 <code class="docutils literal notranslate"><span class="pre">view</span></code> 或 <code class="docutils literal notranslate"><span class="pre">change</span></code> 权限才能使用自动完成。</p>
<p>结果的排序和分页由相关的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_ordering" title="django.contrib.admin.ModelAdmin.get_ordering"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_ordering()</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_paginator" title="django.contrib.admin.ModelAdmin.get_paginator"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_paginator()</span></code></a> 方法控制。</p>
<p>在下面的例子中，<code class="docutils literal notranslate"><span class="pre">ChoiceAdmin</span></code> 对 <code class="docutils literal notranslate"><span class="pre">Question</span></code> 有一个 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 的自动完成字段。结果由 <code class="docutils literal notranslate"><span class="pre">question_text</span></code> 字段过滤，并由 <code class="docutils literal notranslate"><span class="pre">date_created</span></code> 字段排序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">QuestionAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;date_created&#39;</span><span class="p">]</span>
    <span class="n">search_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;question_text&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">ChoiceAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">autocomplete_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;question&#39;</span><span class="p">]</span>
</pre></div>
</div>
<div class="admonition-performance-considerations-for-large-datasets admonition">
<p class="first admonition-title">大型数据集的性能考虑</p>
<p>使用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.ordering" title="django.contrib.admin.ModelAdmin.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.ordering</span></code></a> 排序可能会导致性能问题，因为在一个大的查询集上排序会很慢。</p>
<p>此外，如果你的搜索字段包括没有被数据库索引的字段，你可能会在极大的表上遇到性能不佳的情况。</p>
<p>对于这些情况，最好是使用全文索引搜索来编写自己的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_search_results" title="django.contrib.admin.ModelAdmin.get_search_results"><code class="xref py py-func docutils literal notranslate"><span class="pre">ModelAdmin.get_search_results()</span></code></a> 实现。</p>
<p class="last">你可能还想改变非常大的表的 <code class="docutils literal notranslate"><span class="pre">Paginator</span></code>，因为默认的分页器总是执行 <code class="docutils literal notranslate"><span class="pre">count()</span></code> 查询。例如，你可以覆盖 <code class="docutils literal notranslate"><span class="pre">Paginator.count</span></code> 属性的默认实现。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.raw_id_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">raw_id_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.raw_id_fields" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，Django 的管理员对 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 的字段使用选择框接口（&lt;select&gt;）。有时候，你不想产生必须选择所有相关的实例来显示在下拉框中的开销。</p>
<p><code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> 是你想改变为 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 的 <code class="docutils literal notranslate"><span class="pre">Input</span></code> 部件的字段列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">raw_id_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;newspaper&quot;</span><span class="p">,)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> 的 <code class="docutils literal notranslate"><span class="pre">Input</span></code> 部件应该包含一个主键，如果该字段是 <code class="docutils literal notranslate"><span class="pre">`ForeignKey`</span></code>。或者是一个逗号分隔的值列表，如果该字段是 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code>。 <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> 部件在字段旁边显示一个放大镜按钮，允许用户搜索和选择一个值：</p>
<img alt="../../../_images/raw_id_fields.png" src="../../../_images/raw_id_fields.png" />
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.readonly_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">readonly_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，管理会将所有字段显示为可编辑。该选项中的任何字段（应该是 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code>）将按原样显示其数据，不可编辑；它们也被排除在用于创建和编辑的 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 中。请注意，当指定 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> 或 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fieldsets</span></code></a> 时，只读字段必须存在才能显示（否则将被忽略）。</p>
<p>如果没有通过 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> 或 <code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fielets</span></code> 定义明确的顺序就使用 <code class="docutils literal notranslate"><span class="pre">readonly_fields</span></code>，它们将在所有可编辑字段之后最后添加。</p>
<p>一个只读字段不仅可以显示模型字段的数据，还可以显示模型的方法或 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 类本身的方法的输出。这与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display</span></code></a> 的行为方式非常相似。这提供了一种方法来使用管理员接口来提供被编辑对象的状态反馈，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="kn">import</span> <span class="n">format_html_join</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="kn">import</span> <span class="n">mark_safe</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">readonly_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;address_report&#39;</span><span class="p">,)</span>

    <span class="c1"># description functions like a model field&#39;s verbose_name</span>
    <span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Address&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">address_report</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="c1"># assuming get_full_address() returns a list of strings</span>
        <span class="c1"># for each line of the address and you want to separate each</span>
        <span class="c1"># line by a linebreak</span>
        <span class="k">return</span> <span class="n">format_html_join</span><span class="p">(</span>
            <span class="n">mark_safe</span><span class="p">(</span><span class="s1">&#39;&lt;br&gt;&#39;</span><span class="p">),</span>
            <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span><span class="p">,</span>
            <span class="p">((</span><span class="n">line</span><span class="p">,)</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="n">get_full_address</span><span class="p">()),</span>
        <span class="p">)</span> <span class="ow">or</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s2">&quot;&lt;span class=&#39;errors&#39;&gt;I can&#39;t determine this address.&lt;/span&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_as">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_as</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_as" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">save_as</span></code>，在管理更改表格时启用 “另存为新” 功能。</p>
<p>通常情况下，对象有三个保存选项。“保存”、“保存并继续编辑” 和 “保存并添加另一个”。如果 <code class="docutils literal notranslate"><span class="pre">save_as</span></code> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，则 “保存并添加另一个” 将被 “另存为新” 按钮所取代，该按钮将创建一个新的对象（具有新的 ID），而不是更新现有的对象。</p>
<p>默认情况下，<code class="docutils literal notranslate"><span class="pre">save_as</span></code> 被设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_as_continue">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_as_continue</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_as_continue" title="永久链接至目标">¶</a></dt>
<dd><p>当 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.save_as" title="django.contrib.admin.ModelAdmin.save_as"><code class="xref py py-attr docutils literal notranslate"><span class="pre">save_as=True</span></code></a> 时，保存新对象后默认重定向到该对象的变更视图。如果设置 <code class="docutils literal notranslate"><span class="pre">save_as_continue=False</span></code>，则重定向到变更列表视图。</p>
<p>默认情况下，<code class="docutils literal notranslate"><span class="pre">save_as_continue</span></code> 被设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.save_on_top">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_on_top</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_on_top" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">save_on_top</span></code> 来在你的管理更改表格的顶部添加保存按钮。</p>
<p>通常情况下，保存按钮只出现在表格的底部。如果设置 <code class="docutils literal notranslate"><span class="pre">save_on_top</span></code>，按钮将同时出现在顶部和底部。</p>
<p>默认情况下，<code class="docutils literal notranslate"><span class="pre">save_on_top</span></code> 被设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.search_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">search_fields</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.search_fields" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">search_fields</span></code>，在管理更改列表页面上启用搜索框。这应该被设置为字段名的列表，每当有人在该文本框中提交搜索查询时，就会被搜索到。</p>
<p>这些字段应该是某种文本字段，如 <code class="docutils literal notranslate"><span class="pre">CharField</span></code> 或 <code class="docutils literal notranslate"><span class="pre">TextField</span></code>。你也可以对 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 进行相关查询，并使用查找 API “follow” 符号：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">search_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;foreign_key__related_fieldname&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>例如，如果你有一个有作者的博客条目，下面的定义将可以通过作者的电子邮件地址搜索博客条目：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">search_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;user__email&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>当有人在管理搜索框中进行搜索时，Django 会将搜索查询拆分成多个词，并返回所有包含这些词的对象，不区分大小写（使用 <a class="reference internal" href="../../models/querysets.html#std:fieldlookup-icontains"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">icontains</span></code></a> 查找），其中每个词必须在 <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> 中至少有一个。例如，如果 <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">['first_name',</span> <span class="pre">'last_name']</span></code>，用户搜索 <code class="docutils literal notranslate"><span class="pre">john</span> <span class="pre">lennon'</span></code>，Django 会做相当于这个 SQL <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> 子句。</p>
<div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">WHERE</span> <span class="p">(</span><span class="n">first_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%john%&#39;</span> <span class="k">OR</span> <span class="n">last_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%john%&#39;</span><span class="p">)</span>
<span class="k">AND</span> <span class="p">(</span><span class="n">first_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%lennon%&#39;</span> <span class="k">OR</span> <span class="n">last_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%lennon%&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>搜索查询可以包含带空格的引号短语。例如，如果用户搜索 <code class="docutils literal notranslate"><span class="pre">&quot;john</span> <span class="pre">winston&quot;</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'john</span> <span class="pre">winston'</span></code>，Django 会做相当于这个 SQL 的 <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> 子句：</p>
<div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">WHERE</span> <span class="p">(</span><span class="n">first_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%john winston%&#39;</span> <span class="k">OR</span> <span class="n">last_name</span> <span class="k">ILIKE</span> <span class="s1">&#39;%john winston%&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你不想使用 <code class="docutils literal notranslate"><span class="pre">icontains</span></code> 作为查找，你可以通过附加字段来使用任何查找。例如，你可以通过设置 <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> 为 <code class="docutils literal notranslate"><span class="pre">['first_name__exact']</span></code> 来使用 <a class="reference internal" href="../../models/querysets.html#std:fieldlookup-exact"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">exact</span></code></a>。</p>
<p>还可以使用一些（较老的）快捷方式来指定字段查找。你可以在 <code class="docutils literal notranslate"><span class="pre">search_fields</span></code> 中的字段前加上以下字符，相当于在字段中加上 <code class="docutils literal notranslate"><span class="pre">__&lt;lookup&gt;</span></code>。</p>
<table class="docutils">
<colgroup>
<col width="23%" />
<col width="77%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">前缀</th>
<th class="head">查找</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>^</td>
<td><a class="reference internal" href="../../models/querysets.html#std:fieldlookup-startswith"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">startswith</span></code></a></td>
</tr>
<tr class="row-odd"><td>=</td>
<td><a class="reference internal" href="../../models/querysets.html#std:fieldlookup-iexact"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">iexact</span></code></a></td>
</tr>
<tr class="row-even"><td>&#64;</td>
<td><a class="reference internal" href="../postgres/search.html#std:fieldlookup-search"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">search</span></code></a></td>
</tr>
<tr class="row-odd"><td>None</td>
<td><a class="reference internal" href="../../models/querysets.html#std:fieldlookup-icontains"><code class="xref std std-lookup docutils literal notranslate"><span class="pre">icontains</span></code></a></td>
</tr>
</tbody>
</table>
<p>如果你需要自定义搜索，你可以使用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_search_results" title="django.contrib.admin.ModelAdmin.get_search_results"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_search_results()</span></code></a> 来提供额外的或替代的搜索行为。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>增加了对带空格的引号短语的搜索支持。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.show_full_result_count">
<code class="descclassname">ModelAdmin.</code><code class="descname">show_full_result_count</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.show_full_result_count" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">show_full_result_count</span></code> 来控制是否应该在过滤后的管理页面上显示全部对象的数量（例如： <code class="docutils literal notranslate"><span class="pre">99</span> <span class="pre">results</span> <span class="pre">(103</span> <span class="pre">total)</span></code>）。如果这个选项被设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，则会显示 <code class="docutils literal notranslate"><span class="pre">99</span> <span class="pre">results</span> <span class="pre">(Show</span> <span class="pre">all)</span></code> 这样的文字。</p>
<p>默认的 <code class="docutils literal notranslate"><span class="pre">show_full_result_count=True</span></code> 会生成一个对表进行完整计数的查询，如果表包含大量的行，那么这个查询可能会很昂贵。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.sortable_by">
<code class="descclassname">ModelAdmin.</code><code class="descname">sortable_by</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.sortable_by" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，变更列表页面允许按 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> 中指定的所有模型字段（以及使用 <a class="reference internal" href="#django.contrib.admin.display" title="django.contrib.admin.display"><code class="xref py py-func docutils literal notranslate"><span class="pre">display()</span></code></a> 装饰器的 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 参数或具有 <code class="docutils literal notranslate"><span class="pre">admin_order_field</span></code> 属性的可调用对象）进行排序。</p>
<p>如果你想禁止对某些列进行排序，请将 <code class="docutils literal notranslate"><span class="pre">sortable_by</span></code> 设置为你想排序的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> 子集的一个集合（例如 <code class="docutils literal notranslate"><span class="pre">list`</span></code>、<code class="docutils literal notranslate"><span class="pre">tuple</span></code> 或 <code class="docutils literal notranslate"><span class="pre">set</span></code>）。一个空的集合会禁用所有列的排序。</p>
<p>如果你需要动态地指定这个列表，可以实现一个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_sortable_by" title="django.contrib.admin.ModelAdmin.get_sortable_by"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_sortable_by()</span></code></a> 方法来代替。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.view_on_site">
<code class="descclassname">ModelAdmin.</code><code class="descname">view_on_site</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.view_on_site" title="永久链接至目标">¶</a></dt>
<dd><p>设置 <code class="docutils literal notranslate"><span class="pre">view_on_site</span></code> 来控制是否显示 “在站点上查看” 链接。这个链接应该把你带到一个可以显示保存对象的 URL。</p>
<p>这个值可以是一个布尔标志，也可以是一个可调用对象。如果 <code class="docutils literal notranslate"><span class="pre">True</span></code> （默认），对象的 <a class="reference internal" href="../../models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a> 方法将被用来生成网址。</p>
<p>如果你的模型有一个 <a class="reference internal" href="../../models/instances.html#django.db.models.Model.get_absolute_url" title="django.db.models.Model.get_absolute_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_absolute_url()</span></code></a> 方法，但你不想让 “在站点上查看” 按钮出现，你只需要将 <code class="docutils literal notranslate"><span class="pre">view_on_site</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">view_on_site</span> <span class="o">=</span> <span class="kc">False</span>
</pre></div>
</div>
<p>如果它是一个可调用对象，它接受模型实例作为参数。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">reverse</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">view_on_site</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="n">url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;person-detail&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;slug&#39;</span><span class="p">:</span> <span class="n">obj</span><span class="o">.</span><span class="n">slug</span><span class="p">})</span>
        <span class="k">return</span> <span class="s1">&#39;https://example.com&#39;</span> <span class="o">+</span> <span class="n">url</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="s-custom-template-options">
<span id="custom-template-options"></span><h4>自定义模板选项<a class="headerlink" href="#custom-template-options" title="永久链接至标题">¶</a></h4>
<p><a class="reference internal" href="#admin-overriding-templates"><span class="std std-ref">覆盖管理模板</span></a> 部分描述了如何覆盖或扩展默认的管理模板。 使用以下选项来覆盖 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 视图使用的默认模板。</p>
<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.add_form_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">add_form_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.add_form_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a> 使用。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.change_form_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_form_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_form_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a> 使用。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.change_list_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_list_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_list_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.changelist_view" title="django.contrib.admin.ModelAdmin.changelist_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">changelist_view()</span></code></a> 使用。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.delete_confirmation_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_confirmation_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_confirmation_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_view" title="django.contrib.admin.ModelAdmin.delete_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_view()</span></code></a> 用于在删除一个或多个对象时显示确认页面。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.delete_selected_confirmation_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_selected_confirmation_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_selected_confirmation_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <code class="docutils literal notranslate"><span class="pre">delete_selected</span></code> 动作方法使用，在删除一个或多个对象时显示确认页面。参见 <a class="reference internal" href="actions.html"><span class="doc">动作文档</span></a>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.object_history_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">object_history_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.object_history_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.history_view" title="django.contrib.admin.ModelAdmin.history_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">history_view()</span></code></a> 使用。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.ModelAdmin.popup_response_template">
<code class="descclassname">ModelAdmin.</code><code class="descname">popup_response_template</code><a class="headerlink" href="#django.contrib.admin.ModelAdmin.popup_response_template" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_add" title="django.contrib.admin.ModelAdmin.response_add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_add()</span></code></a>、 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_change" title="django.contrib.admin.ModelAdmin.response_change"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_change()</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.response_delete" title="django.contrib.admin.ModelAdmin.response_delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">response_delete()</span></code></a> 使用的自定义模板的路径。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-modeladmin-methods">
<span id="s-model-admin-methods"></span><span id="modeladmin-methods"></span><span id="model-admin-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 方法<a class="headerlink" href="#modeladmin-methods" title="永久链接至标题">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">当覆盖 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.save_model" title="django.contrib.admin.ModelAdmin.save_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.save_model()</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_model" title="django.contrib.admin.ModelAdmin.delete_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.delete_model()</span></code></a> 时，你的代码必须保存／删除对象。它们不是为了否决的目的，而是允许你执行额外的操作。</p>
</div>
<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_model">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_model</code>(<em>request</em>, <em>obj</em>, <em>form</em>, <em>change</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_model" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">save_model</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>、一个模型实例、一个 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 实例和一个基于是否添加或更改对象的布尔值。覆盖这个方法可以进行保存前或保存后的操作。调用 <code class="docutils literal notranslate"><span class="pre">super().save_model()</span></code> 使用 <a class="reference internal" href="../../models/instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Model.save()</span></code></a> 保存对象。</p>
<p>例如，在保存之前将 <code class="docutils literal notranslate"><span class="pre">request.user</span></code> 附加到对象上：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">save_model</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">change</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.delete_model">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_model</code>(<em>request</em>, <em>obj</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_model" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">delete_model</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和一个模型实例。覆盖该方法可以进行删除前或删除后的操作。调用 <code class="docutils literal notranslate"><span class="pre">super().delete_model()</span></code> 使用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">Model.delete`()</span></code> 删除对象。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.delete_queryset">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_queryset</code>(<em>request</em>, <em>queryset</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_queryset" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">delete_queryset()</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和一个 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 要删除的对象。重写该方法，可以自定义 “删除选定对象” 的删除过程 <a class="reference internal" href="actions.html"><span class="doc">动作</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_formset">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_formset</code>(<em>request</em>, <em>form</em>, <em>formset</em>, <em>change</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_formset" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">save_formset</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>、父 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 实例和一个基于是否添加或更改父对象的布尔值。</p>
<p>例如，将 <code class="docutils literal notranslate"><span class="pre">request.user</span></code> 附加到每一个改变了的表单集模型实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">save_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">formset</span><span class="p">,</span> <span class="n">change</span><span class="p">):</span>
        <span class="n">instances</span> <span class="o">=</span> <span class="n">formset</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">formset</span><span class="o">.</span><span class="n">deleted_objects</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">:</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="n">formset</span><span class="o">.</span><span class="n">save_m2m</span><span class="p">()</span>
</pre></div>
</div>
<p>另见 <a class="reference internal" href="../../../topics/forms/modelforms.html#saving-objects-in-the-formset"><span class="std std-ref">在表单集中保存对象</span></a>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_ordering">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_ordering</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_ordering" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_ordering</span></code> 方法以 <code class="docutils literal notranslate"><span class="pre">request</span></code> 为参数，并期望返回一个类似于 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.ordering" title="django.contrib.admin.ModelAdmin.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a> 属性的 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 的排序。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">get_ordering</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;rank&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_search_results">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_search_results</code>(<em>request</em>, <em>queryset</em>, <em>search_term</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_search_results" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_search_results</span></code> 方法将显示的对象列表修改为符合所提供的搜索词的对象。它接受请求、应用当前过滤器的查询集和用户提供的搜索词。它返回一个元组，其中包含一个修改后实现搜索的查询集，以及一个布尔值，表示结果是否包含重复。</p>
<p>默认的实现是搜索 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.search_fields" title="django.contrib.admin.ModelAdmin.search_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.search_fields</span></code></a> 中命名的字段。</p>
<p>这个方法可以用你自己的自定义搜索方法来覆盖。例如，你可能希望通过一个整数字段进行搜索，或使用外部工具，如 Solr 或 Haystack。你必须确定由你的搜索方法实现的查询集变化是否会在结果中引入重复，并在返回值的第二个元素中返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p>例如，如果要按 <code class="docutils literal notranslate"><span class="pre">name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">age</span></code> 进行搜索，你可以使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;age&#39;</span><span class="p">)</span>
    <span class="n">search_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">get_search_results</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">search_term</span><span class="p">):</span>
        <span class="n">queryset</span><span class="p">,</span> <span class="n">may_have_duplicates</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_search_results</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">search_term</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">search_term_as_int</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">search_term</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">queryset</span> <span class="o">|=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">age</span><span class="o">=</span><span class="n">search_term_as_int</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">may_have_duplicates</span>
</pre></div>
</div>
<p>这个实现比 <code class="docutils literal notranslate"><span class="pre">search_fields</span> <span class="pre">=</span> <span class="pre">('name',</span> <span class="pre">'=age')</span></code> 更有效，因为后者的结果是对数字字段进行字符串比较，例如 <code class="docutils literal notranslate"><span class="pre">...</span> <span class="pre">OR</span> <span class="pre">UPPER(&quot;polls_choice&quot;.&quot;votes&quot;::text)</span> <span class="pre">=</span> <span class="pre">UPPER('4')</span></code> 在 PostgreSQL 上。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.save_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">save_related</code>(<em>request</em>, <em>form</em>, <em>formsets</em>, <em>change</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.save_related" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">save_related</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>、父 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 实例、内联表单集列表和一个基于父对象是否被添加或更改的布尔值。在这里可以对父对象相关的对象进行任何保存前或保存后的操作。请注意，此时父对象及其窗体已经被保存。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_autocomplete_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_autocomplete_fields</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_autocomplete_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_autocomplete_fields()</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，预计将返回一个 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 字段名，这些字段名将与自动完成部件一起显示，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.autocomplete_fields" title="django.contrib.admin.ModelAdmin.autocomplete_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.autocomplete_fields</span></code></a> 部分所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_readonly_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_readonly_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_readonly_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_readonly_fields</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj``（或者在添加表单中给定</span> <span class="pre">``None</span></code>），并期望返回一个 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 的字段名，这些字段名将被显示为只读，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.readonly_fields</span></code></a> 部分所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_prepopulated_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_prepopulated_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_prepopulated_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_prepopulated_fields</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或者在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），并期望返回一个 <code class="docutils literal notranslate"><span class="pre">dictionary</span></code>，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.prepopulated_fields</span></code></a> 一节所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_display">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_display</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_display" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_list_display</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，预计将返回一个 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 的字段名，这些字段名将显示在变更列表视图上，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display</span></code></a> 一节所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_display_links">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_display_links</code>(<em>request</em>, <em>list_display</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_display_links" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_list_display_links</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和由 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_display" title="django.contrib.admin.ModelAdmin.get_list_display"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_list_display()</span></code></a> 返回的 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code>。如 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display_links" title="django.contrib.admin.ModelAdmin.list_display_links"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_display_links</span></code></a> 一节所述，预计它将返回变化列表中的 <code class="docutils literal notranslate"><span class="pre">None</span></code> 或 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 字段名，这些字段名将被链接到变化视图。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_exclude">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_exclude</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_exclude" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_exclude</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或者在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），并期望返回一个字段列表，如 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.exclude</span></code></a> 中所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_fields</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_fields</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或者在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），并期望返回一个字段列表，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fields</span></code></a> 一节所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_fieldsets">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_fieldsets</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_fieldsets" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_fieldsets</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或者在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），预计将返回一个双元组列表，其中每个双元组代表管理表单页面上的 <code class="docutils literal notranslate"><span class="pre">&lt;fieldset&gt;</span></code>，如上面 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.fieldsets</span></code></a> 部分所述。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_filter">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_filter</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_filter" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_list_filter</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，并期望返回与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> 属性相同的序列类型。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_list_select_related">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_list_select_related</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_list_select_related" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_list_select_related</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，应该像 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_select_related" title="django.contrib.admin.ModelAdmin.list_select_related"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_select_related</span></code></a> 那样返回一个布尔值或列表。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_search_fields">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_search_fields</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_search_fields" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_search_fields</span></code> 方法被赋予 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，并期望返回与 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.search_fields" title="django.contrib.admin.ModelAdmin.search_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">search_fields</span></code></a> 属性相同的序列类型。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_sortable_by">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_sortable_by</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_sortable_by" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_sortable_by()</span></code> 方法被传递给 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code>，并期望返回一个字段名的集合（例如 <code class="docutils literal notranslate"><span class="pre">list</span></code>、<code class="docutils literal notranslate"><span class="pre">tuple</span></code> 或 <code class="docutils literal notranslate"><span class="pre">set</span></code>），这些字段名将在更改列表页中被排序。</p>
<p>如果设置了，它的默认实现将返回 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.sortable_by" title="django.contrib.admin.ModelAdmin.sortable_by"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sortable_by</span></code></a>，否则将服从 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_list_display" title="django.contrib.admin.ModelAdmin.get_list_display"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_list_display()</span></code></a>。</p>
<p>例如，要防止一列或多列无法排序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">get_sortable_by</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">get_list_display</span><span class="p">(</span><span class="n">request</span><span class="p">)}</span> <span class="o">-</span> <span class="p">{</span><span class="s1">&#39;rank&#39;</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_inline_instances">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_inline_instances</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_inline_instances" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_inline_instances</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或者在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），并期望返回一个 <code class="docutils literal notranslate"><span class="pre">list</span></code> 或 <code class="docutils literal notranslate"><span class="pre">tuple</span></code> 的 :class:<a href="#id1"><span class="problematic" id="id2">``</span></a>~django.contrib.admin.InlineModelAdmin` 对象，如下文 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a> 部分所述。例如，以下内容将返回没有基于添加、更改、删除和查看权限的默认过滤的内联：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MyInline</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">get_inline_instances</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">inline</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="p">)</span> <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">inlines</span><span class="p">]</span>
</pre></div>
</div>
<p>如果你覆盖了这个方法，请确保返回的内联是 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.inlines" title="django.contrib.admin.ModelAdmin.inlines"><code class="xref py py-attr docutils literal notranslate"><span class="pre">inlines</span></code></a> 中定义的类的实例，否则在添加相关对象时可能会遇到 “Bad Request” 错误。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_inlines">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_inlines</code>(<em>request</em>, <em>obj</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_inlines" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">get_inlines</span></code> 方法是给定 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 和被编辑的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> （或在添加表单中给定 <code class="docutils literal notranslate"><span class="pre">None</span></code>），并期望返回一个可迭代对象的内联。你可以覆盖这个方法，根据请求或模型实例动态添加内联，而不是在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.inlines" title="django.contrib.admin.ModelAdmin.inlines"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.inlines</span></code></a> 中指定它们。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_urls">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_urls</code>()<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_urls" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上的 <code class="docutils literal notranslate"><span class="pre">get_urls</span></code> 方法以与 URLconf 相同的方式返回用于该 ModelAdmin 的 URL。 因此，你可以按照 <a class="reference internal" href="../../../topics/http/urls.html"><span class="doc">URL调度器</span></a> 中的文档来扩展它们：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.template.response</span> <span class="kn">import</span> <span class="n">TemplateResponse</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">urls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_urls</span><span class="p">()</span>
        <span class="n">my_urls</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;my_view/&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">),</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">my_urls</span> <span class="o">+</span> <span class="n">urls</span>

    <span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
           <span class="c1"># Include common variables for rendering the admin template.</span>
           <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">each_context</span><span class="p">(</span><span class="n">request</span><span class="p">),</span>
           <span class="c1"># Anything else you want in the context...</span>
           <span class="n">key</span><span class="o">=</span><span class="n">value</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;sometemplate.html&quot;</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你想使用管理布局，从 <code class="docutils literal notranslate"><span class="pre">admin/base_site.html</span></code> 扩展：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;admin/base_site.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>请注意，自定义模式包含在常规的管理 URL <em>之前</em>：管理 URL 模式是非常宽松的，几乎可以匹配任何东西，所以你通常会希望将你的自定义 URL 添加到内置的 URL 中。</p>
<p class="last">在这个例子中，<code class="docutils literal notranslate"><span class="pre">my_view</span></code> 将在 <code class="docutils literal notranslate"><span class="pre">/admin/myapp/mymodel/my_view/</span></code> 中被访问（假设在 <code class="docutils literal notranslate"><span class="pre">/admin/</span></code> 中包含了管理的 URL）。</p>
</div>
<p>然而，上面注册的 <code class="docutils literal notranslate"><span class="pre">self.my_view</span></code> 函数存在两个问题：</p>
<ul class="simple">
<li>它将 <em>不</em> 进行任何权限检查，所以它将对普通公众开放。</li>
<li>它将 <em>不</em> 提供任何头信息以防止缓存。这意味着如果页面从数据库中检索数据，而缓存中间件是活动的，页面可能会显示过时的信息。</li>
</ul>
<p>由于这通常不是你想要的，Django 提供了一个方便的包装器来检查权限并将视图标记为不可缓存。这个封装器是 <code class="docutils literal notranslate"><span class="pre">AdminSite.admin_view()</span></code> （即 <code class="docutils literal notranslate"><span class="pre">self.admin_site.admin_view</span></code> 在一个 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 实例中）；使用它就像这样：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_urls</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">urls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_urls</span><span class="p">()</span>
        <span class="n">my_urls</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">path</span><span class="p">(</span><span class="s1">&#39;my_view/&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">))</span>
        <span class="p">]</span>
        <span class="k">return</span> <span class="n">my_urls</span> <span class="o">+</span> <span class="n">urls</span>
</pre></div>
</div>
<p>请注意上面第五行的包装的视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s1">&#39;my_view/&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">))</span>
</pre></div>
</div>
<p>这个封装将保护 <code class="docutils literal notranslate"><span class="pre">self.my_view</span></code> 不被未经授权的访问，并将应用 <a class="reference internal" href="../../../topics/http/decorators.html#django.views.decorators.cache.never_cache" title="django.views.decorators.cache.never_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.views.decorators.cache.never_cache()</span></code></a> 装饰器来确保在缓存中间件处于活动状态时不被缓存。</p>
<p>如果页面是可缓存的，但你仍然希望进行权限检查，你可以传递一个 <code class="docutils literal notranslate"><span class="pre">cacheable=True</span></code> 参数到 <code class="docutils literal notranslate"><span class="pre">AdminSite.admin_view()</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">path</span><span class="p">(</span><span class="s1">&#39;my_view/&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">admin_site</span><span class="o">.</span><span class="n">admin_view</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">my_view</span><span class="p">,</span> <span class="n">cacheable</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 视图有 <code class="docutils literal notranslate"><span class="pre">model_admin</span></code> 属性。其他 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 视图有 <code class="docutils literal notranslate"><span class="pre">admin_site</span></code> 属性。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_form">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_form</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_form" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 类，用于管理员添加和更改视图，参见 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a>。</p>
<p>基本实现使用 <a class="reference internal" href="../../forms/models.html#django.forms.models.modelform_factory" title="django.forms.models.modelform_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">modelform_factory()</span></code></a> 来子类 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.form" title="django.contrib.admin.ModelAdmin.form"><code class="xref py py-attr docutils literal notranslate"><span class="pre">form</span></code></a>，通过 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exclude</span></code></a> 等属性进行修改。所以，例如，如果你想为超级用户提供额外的字段，你可以像这样换一个不同的基本表单：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;form&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">MySuperuserForm</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>你也可以直接返回一个自定义的 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 类。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_formsets_with_inlines">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_formsets_with_inlines</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="永久链接至目标">¶</a></dt>
<dd><p>产生 (<code class="docutils literal notranslate"><span class="pre">FormSet</span></code>, <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a>) 对，用于管理添加和更改视图。</p>
<p>例如，如果你想只在变化视图中显示一个特定的内联，你可以覆盖 <code class="docutils literal notranslate"><span class="pre">get_formsets_with_inlines</span></code> 如下：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyInline</span><span class="p">,</span> <span class="n">SomeOtherInline</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_formsets_with_inlines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">inline</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_inline_instances</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
            <span class="c1"># hide MyInline in the add view</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inline</span><span class="p">,</span> <span class="n">MyInline</span><span class="p">)</span> <span class="ow">or</span> <span class="n">obj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">yield</span> <span class="n">inline</span><span class="o">.</span><span class="n">get_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="p">),</span> <span class="n">inline</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_foreignkey">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_foreignkey</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_foreignkey" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上的 <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> 方法允许你覆盖外键字段的默认 formfield。例如，要根据用户返回这个外键字段的对象子集：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_foreignkey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;car&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;queryset&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Car</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield_for_foreignkey</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>这使用 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 实例过滤 <code class="docutils literal notranslate"><span class="pre">Car</span></code> 外键字段，只显示 <code class="docutils literal notranslate"><span class="pre">User</span></code> 实例拥有的汽车。</p>
<p>对于更复杂的过滤器，你可以使用 <code class="docutils literal notranslate"><span class="pre">ModelForm.__init__()</span></code> 方法来基于你的模型的 <code class="docutils literal notranslate"><span class="pre">instance</span></code> 进行过滤（参见 <a class="reference internal" href="../../forms/fields.html#fields-which-handle-relationships"><span class="std std-ref">处理关系的字段</span></a>）。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">CountryAdminForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s1">&#39;capital&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="o">.</span><span class="n">cities</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">CountryAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">CountryAdminForm</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_manytomany">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_manytomany</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_manytomany" title="永久链接至目标">¶</a></dt>
<dd><p>与 <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> 方法一样，<code class="docutils literal notranslate"><span class="pre">formfield_for_manytomany</span></code> 方法也可以被重写，以改变多对多字段的默认字段。例如，如果一个车主可以拥有多辆汽车，而汽车可以属于多个车主 —— 多对多关系 —— 你可以过滤 <code class="docutils literal notranslate"><span class="pre">Car</span></code> 外键字段，只显示 <code class="docutils literal notranslate"><span class="pre">User</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;cars&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;queryset&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Car</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.formfield_for_choice_field">
<code class="descclassname">ModelAdmin.</code><code class="descname">formfield_for_choice_field</code>(<em>db_field</em>, <em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.formfield_for_choice_field" title="永久链接至目标">¶</a></dt>
<dd><p>与 <code class="docutils literal notranslate"><span class="pre">formfield_for_foreignkey</span></code> 和 <code class="docutils literal notranslate"><span class="pre">formfield_for_manytomany</span></code> 方法一样，<code class="docutils literal notranslate"><span class="pre">formfield_for_choice_field</span></code> 方法可以被重写，以改变已声明选择的字段的默认字段。例如，如果超级用户的选择与普通员工的选择不同，你可以按以下步骤进行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">formfield_for_choice_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;status&quot;</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;choices&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                <span class="p">(</span><span class="s1">&#39;accepted&#39;</span><span class="p">,</span> <span class="s1">&#39;Accepted&#39;</span><span class="p">),</span>
                <span class="p">(</span><span class="s1">&#39;denied&#39;</span><span class="p">,</span> <span class="s1">&#39;Denied&#39;</span><span class="p">),</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
                <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;choices&#39;</span><span class="p">]</span> <span class="o">+=</span> <span class="p">((</span><span class="s1">&#39;ready&#39;</span><span class="p">,</span> <span class="s1">&#39;Ready for deployment&#39;</span><span class="p">),)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield_for_choice_field</span><span class="p">(</span><span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">注解</p>
<p class="last">任何在表单字段上设置的 <code class="docutils literal notranslate"><span class="pre">choices</span></code> 属性将只限于表单字段。如果模型上对应的字段设置了选择，那么提供给表单的选择必须是这些选择的有效子集，否则在保存前对模型本身进行验证时，表单提交将以一个 <a class="reference internal" href="../../exceptions.html#django.core.exceptions.ValidationError" title="django.core.exceptions.ValidationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValidationError</span></code></a> 失败。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist</code>(<em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist" title="永久链接至目标">¶</a></dt>
<dd><p>返回用于列表的 <code class="docutils literal notranslate"><span class="pre">Changelist</span></code> 类。默认情况下，使用的是 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.views.main.ChangeList</span></code>。通过继承这个类，你可以改变列表的行为。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist_form">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist_form</code>(<em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist_form" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 类，供变更列表页面中的 <code class="docutils literal notranslate"><span class="pre">Formset</span></code> 使用。要使用一个自定义表单，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">MyForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_changelist_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">MyForm</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">注解</p>
<p class="last">如果你在 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelForm</span></code></a> 上定义了 <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> 属性，你也必须定义 <code class="docutils literal notranslate"><span class="pre">Meta.fields</span></code> 属性（或 <code class="docutils literal notranslate"><span class="pre">Meta.exclude</span></code> 属性）。然而，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 会忽略这个值，用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_editable" title="django.contrib.admin.ModelAdmin.list_editable"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.list_editable</span></code></a> 属性来覆盖它。最简单的解决办法是省略 <code class="docutils literal notranslate"><span class="pre">Meta.model</span></code> 属性，因为 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 将提供正确的模型使用。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changelist_formset">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changelist_formset</code>(<em>request</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changelist_formset" title="永久链接至目标">¶</a></dt>
<dd><p>如果使用了 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_editable" title="django.contrib.admin.ModelAdmin.list_editable"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_editable</span></code></a>，则返回一个 <a class="reference internal" href="../../../topics/forms/modelforms.html#model-formsets"><span class="std std-ref">ModelFormSet</span></a> 类，供变更列表页面使用。要使用自定义表单集，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.forms</span> <span class="kn">import</span> <span class="n">BaseModelFormSet</span>

<span class="k">class</span> <span class="nc">MyAdminFormSet</span><span class="p">(</span><span class="n">BaseModelFormSet</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_changelist_formset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;formset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">MyAdminFormSet</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_changelist_formset</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.lookup_allowed">
<code class="descclassname">ModelAdmin.</code><code class="descname">lookup_allowed</code>(<em>lookup</em>, <em>value</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.lookup_allowed" title="永久链接至目标">¶</a></dt>
<dd><p>变更列表页面中的对象可以通过 URL 的查询字符串进行过滤。例如 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> 就是这样工作的。这些查询类似于 <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.filter" title="django.db.models.query.QuerySet.filter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.filter()</span></code></a> （例如 <code class="docutils literal notranslate"><span class="pre">user__email=user&#64;example.com</span></code>）。由于用户可以对查询字符串中的查找进行操作，因此必须对它们进行处理，以防止未经授权的数据暴露。</p>
<p><code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> 方法从查询字符串（如 <code class="docutils literal notranslate"><span class="pre">'user__email'</span></code>）和相应的值（如``'<a class="reference external" href="mailto:user&#37;&#52;&#48;example&#46;com">user<span>&#64;</span>example<span>&#46;</span>com</a>'<code class="docutils literal notranslate"><span class="pre">）中得到一个查找路径，并返回一个布尔值，表示是否允许使用参数过滤变更列表的</span> <span class="pre">``QuerySet</span></code>。如果 <code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>，则会引发 <code class="docutils literal notranslate"><span class="pre">DisallowedModelAdminLookup</span></code> （<a class="reference internal" href="../../exceptions.html#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SuspiciousOperation</span></code></a> 的子类）。</p>
<p>默认情况下，<code class="docutils literal notranslate"><span class="pre">lookup_allowed()</span></code> 允许访问模型的本地字段、在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_filter" title="django.contrib.admin.ModelAdmin.list_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_filter</span></code></a> 中使用的字段路径（但不包括 <code class="xref py py-meth docutils literal notranslate"> <span class="pre">get_list_filter()</span></code>）中使用的字段路径，以及 <a class="reference internal" href="../../models/fields.html#django.db.models.ForeignKey.limit_choices_to" title="django.db.models.ForeignKey.limit_choices_to"><code class="xref py py-attr docutils literal notranslate"><span class="pre">limit_choices_to</span></code></a> 在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.raw_id_fields" title="django.contrib.admin.ModelAdmin.raw_id_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">raw_id_fields</span></code></a> 中正确运行所需的查找。</p>
<p>重写这个方法来定制你的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 子类允许的查找。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_view_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_view_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_view_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许查看 <code class="docutils literal notranslate"><span class="pre">obj</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。如果 obj 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 或 <code class="docutils literal notranslate"><span class="pre">False</span></code> 表示是否允许查看该类型的对象（例如，<code class="docutils literal notranslate"><span class="pre">False</span></code> 将被解释为当前用户不允许查看该类型的任何对象）。</p>
<p>如果用户有 “更改” 或 “查看” 权限，默认的实现将返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_add_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_add_permission</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_add_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许添加对象，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_change_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_change_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_change_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许编辑 <code class="docutils literal notranslate"><span class="pre">obj</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则应返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。如果 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 或 <code class="docutils literal notranslate"><span class="pre">False</span></code> 表示是否允许编辑该类型对象（例如，<code class="docutils literal notranslate"><span class="pre">False</span></code> 将被解释为当前用户不允许编辑该类型的任何对象）。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_delete_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_delete_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_delete_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许删除 <code class="docutils literal notranslate"><span class="pre">obj</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。如果 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 或 <code class="docutils literal notranslate"><span class="pre">False</span></code>，以表明是否允许删除该类型的对象（例如，<code class="docutils literal notranslate"><span class="pre">False</span></code> 将被解释为当前用户不允许删除该类型的任何对象）。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.has_module_permission">
<code class="descclassname">ModelAdmin.</code><code class="descname">has_module_permission</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.has_module_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许在管理员索引页上显示模块和访问模块的索引页，应该返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。默认使用 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.has_module_perms" title="django.contrib.auth.models.User.has_module_perms"><code class="xref py py-meth docutils literal notranslate"><span class="pre">User.has_module_perms()</span></code></a>。覆盖它并不限制对视图的访问，添加、更改或删除视图， <a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_view_permission" title="django.contrib.admin.ModelAdmin.has_view_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_view_permission()</span></code></a>、 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_add_permission" title="django.contrib.admin.ModelAdmin.has_add_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_add_permission()</span></code></a>、 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_change_permission" title="django.contrib.admin.ModelAdmin.has_change_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_change_permission()</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_delete_permission" title="django.contrib.admin.ModelAdmin.has_delete_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_delete_permission()</span></code></a> 应该用于此。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_queryset">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_queryset</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_queryset" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上的 <code class="docutils literal notranslate"><span class="pre">get_queryset</span></code> 方法返回一个 <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 的所有模型实例，这些实例可以被管理网站编辑。覆盖该方法的一个用例是显示登录用户拥有的对象：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">get_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">qs</span>
        <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">author</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.message_user">
<code class="descclassname">ModelAdmin.</code><code class="descname">message_user</code>(<em>request</em>, <em>message</em>, <em>level=messages.INFO</em>, <em>extra_tags=''</em>, <em>fail_silently=False</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.message_user" title="永久链接至目标">¶</a></dt>
<dd><p>使用 <a class="reference internal" href="../messages.html#module-django.contrib.messages" title="django.contrib.messages: Provides cookie- and session-based temporary message storage."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.messages</span></code></a> 后台向用户发送消息。 参见 <a class="reference internal" href="actions.html#custom-admin-action"><span class="std std-ref">自定义 ModelAdmin 示例</span></a>。</p>
<p>关键字参数允许你改变消息的级别，添加额外的 CSS 标签，或者在没有安装 <code class="docutils literal notranslate"><span class="pre">contrib.messages</span></code> 框架的情况下无声地失败。这些关键字参数与 <a class="reference internal" href="../messages.html#django.contrib.messages.add_message" title="django.contrib.messages.add_message"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.messages.add_message()</span></code></a> 的参数一致，更多细节请参见该函数的文档。一个不同的地方是，除了整数／常量之外，级别还可以作为字符串标签传递。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_paginator">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_paginator</code>(<em>request</em>, <em>queryset</em>, <em>per_page</em>, <em>orphans=0</em>, <em>allow_empty_first_page=True</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_paginator" title="永久链接至目标">¶</a></dt>
<dd><p>返回要用于该视图的分页器实例。默认情况下，实例化一个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.paginator" title="django.contrib.admin.ModelAdmin.paginator"><code class="xref py py-attr docutils literal notranslate"><span class="pre">paginator</span></code></a> 的实例。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_add">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_add</code>(<em>request</em>, <em>obj</em>, <em>post_url_continue=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_add" title="永久链接至目标">¶</a></dt>
<dd><p>确定 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.add_view" title="django.contrib.admin.ModelAdmin.add_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_view()</span></code></a> 阶段的 <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a>。</p>
<p><code class="docutils literal notranslate"><span class="pre">response_add</span></code> 在提交管理表单后，对象和所有相关实例被创建和保存后被调用。你可以覆盖它来改变对象创建后的默认行为。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_change">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_change</code>(<em>request</em>, <em>obj</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_change" title="永久链接至目标">¶</a></dt>
<dd><p>确定 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.change_view" title="django.contrib.admin.ModelAdmin.change_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">change_view()</span></code></a> 阶段的 <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a>。</p>
<p><code class="docutils literal notranslate"><span class="pre">response_change</span></code> 在管理表单提交后，对象和所有相关实例被保存后被调用。你可以覆盖它来改变对象被改变后的默认行为。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.response_delete">
<code class="descclassname">ModelAdmin.</code><code class="descname">response_delete</code>(<em>request</em>, <em>obj_display</em>, <em>obj_id</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.response_delete" title="永久链接至目标">¶</a></dt>
<dd><p>为 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_view" title="django.contrib.admin.ModelAdmin.delete_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_view()</span></code></a> 阶段确定 <a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a>。</p>
<p><code class="docutils literal notranslate"><span class="pre">response_delete</span></code> 在对象被删除后被调用。你可以覆盖它来改变对象被删除后的默认行为。</p>
<p><code class="docutils literal notranslate"><span class="pre">obj_display</span></code> 是删除对象名称的字符串。</p>
<p><code class="docutils literal notranslate"><span class="pre">obj_id</span></code> 是用于检索要删除的对象的序列化标识符。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_changeform_initial_data">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_changeform_initial_data</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_changeform_initial_data" title="永久链接至目标">¶</a></dt>
<dd><p>一个钩子，用于管理更改表格的初始数据。默认情况下，字段的初始值来自 <code class="docutils literal notranslate"><span class="pre">GET</span></code> 参数。例如，<code class="docutils literal notranslate"><span class="pre">?name=initial_value</span></code> 将把 <code class="docutils literal notranslate"><span class="pre">name</span></code> 字段的初始值设置为 <code class="docutils literal notranslate"><span class="pre">initial_value</span></code>。</p>
<p>这个方法应该返回一个形式为 <code class="docutils literal notranslate"><span class="pre">{'fieldname':</span> <span class="pre">'fieldval'}</span></code> 的字典：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_changeform_initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_initial_value&#39;</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_deleted_objects">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_deleted_objects</code>(<em>objs</em>, <em>request</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_deleted_objects" title="永久链接至目标">¶</a></dt>
<dd><p>一个钩子，用于自定义 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.delete_view" title="django.contrib.admin.ModelAdmin.delete_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">delete_view()</span></code></a> 和 “删除已选” <a class="reference internal" href="actions.html"><span class="doc">动作</span></a> 的删除过程。</p>
<p><code class="docutils literal notranslate"><span class="pre">objs</span></code> 参数是要删除的对象（一个 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 或模型实例列表）的等价可迭代对象，<code class="docutils literal notranslate"><span class="pre">request</span></code> 是 <a class="reference internal" href="../../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a>。</p>
<p>这个方法必须返回一个四元元组 <code class="docutils literal notranslate"><span class="pre">(delete_objects,</span> <span class="pre">model_count,</span> <span class="pre">perms_needed,</span> <span class="pre">protected)</span></code>。</p>
<p><code class="docutils literal notranslate"><span class="pre">deleted_objects</span></code> 是一个代表所有将被删除对象的字符串列表。如果有任何相关的对象要删除，则列表是嵌套的，包括这些相关对象。该列表在模板中使用 <a class="reference internal" href="../../templates/builtins.html#std:templatefilter-unordered_list"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">unordered_list</span></code></a> 过滤器进行格式化。</p>
<p><code class="docutils literal notranslate"><span class="pre">model_count</span></code> 是一个将每个模型的 <a class="reference internal" href="../../models/options.html#django.db.models.Options.verbose_name_plural" title="django.db.models.Options.verbose_name_plural"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verbose_name_plural</span></code></a> 映射到将被删除的对象数量的字典。</p>
<p><code class="docutils literal notranslate"><span class="pre">perms_needed</span></code> 是一组 <a class="reference internal" href="../../models/options.html#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verbose_name</span></code></a> 的用户没有权限删除的模型。</p>
<p><code class="docutils literal notranslate"><span class="pre">protected</span></code> 是一个字符串列表，代表所有不能删除的受保护相关对象。该列表显示在模板中。</p>
</dd></dl>

<div class="section" id="s-other-methods">
<span id="other-methods"></span><h4>其他方法<a class="headerlink" href="#other-methods" title="永久链接至标题">¶</a></h4>
<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.add_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">add_view</code>(<em>request</em>, <em>form_url=''</em>, <em>extra_context=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.add_view" title="永久链接至目标">¶</a></dt>
<dd><p>模型实例添加页面的 Django 视图。见下面的说明。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.change_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">change_view</code>(<em>request</em>, <em>object_id</em>, <em>form_url=''</em>, <em>extra_context=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.change_view" title="永久链接至目标">¶</a></dt>
<dd><p>模型实例编辑页面的 Django 视图。见下面的说明。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.changelist_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">changelist_view</code>(<em>request</em>, <em>extra_context=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.changelist_view" title="永久链接至目标">¶</a></dt>
<dd><p>模型实例变更列表／动作页面的 Django 视图。见下面的说明。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.delete_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">delete_view</code>(<em>request</em>, <em>object_id</em>, <em>extra_context=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.delete_view" title="永久链接至目标">¶</a></dt>
<dd><p>模型实例删除确认页面的 Django 视图。参见下面的说明。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.history_view">
<code class="descclassname">ModelAdmin.</code><code class="descname">history_view</code>(<em>request</em>, <em>object_id</em>, <em>extra_context=None</em>)<a class="headerlink" href="#django.contrib.admin.ModelAdmin.history_view" title="永久链接至目标">¶</a></dt>
<dd><p>Django 视图用于显示给定模型实例的修改历史的页面。</p>
</dd></dl>

<p>与上一节中详细介绍的钩子类型的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 方法不同，这五个方法实际上是被设计成作为 Django 视图从管理应用的 URL 调度处理程序中调用，以渲染处理模型实例 CRUD 操作的页面。因此，完全覆盖这些方法将显著改变管理员应用程序的行为。</p>
<p>覆盖这些方法的一个常见原因是为了增强提供给渲染视图的模板的上下文数据。在下面的例子中，更改视图被覆盖，以便为渲染模板提供一些额外的映射数据，否则这些数据将无法使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>

    <span class="c1"># A template for a very customized change view:</span>
    <span class="n">change_form_template</span> <span class="o">=</span> <span class="s1">&#39;admin/myapp/extras/openstreetmap_change_form.html&#39;</span>

    <span class="k">def</span> <span class="nf">get_osm_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># ...</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">change_view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">extra_context</span> <span class="o">=</span> <span class="n">extra_context</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="n">extra_context</span><span class="p">[</span><span class="s1">&#39;osm_data&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_osm_info</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">change_view</span><span class="p">(</span>
            <span class="n">request</span><span class="p">,</span> <span class="n">object_id</span><span class="p">,</span> <span class="n">form_url</span><span class="p">,</span> <span class="n">extra_context</span><span class="o">=</span><span class="n">extra_context</span><span class="p">,</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>这些视图返回 <a class="reference internal" href="../../template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a> 实例，允许你在渲染前轻松定制响应数据。更多细节，请看 <a class="reference internal" href="../../template-response.html"><span class="doc">TemplateResponse 文档</span></a>。</p>
</div>
</div>
<div class="section" id="s-modeladmin-asset-definitions">
<span id="s-id1"></span><span id="modeladmin-asset-definitions"></span><span id="id1"></span><h3><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 静态资源定义<a class="headerlink" href="#modeladmin-asset-definitions" title="永久链接至标题">¶</a></h3>
<p>有时你会想在添加／更改视图时添加一点 CSS 和／或 JavaScript。这可以通过在你的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上使用 <code class="docutils literal notranslate"><span class="pre">Media</span></code> 内类来实现：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="k">class</span> <span class="nc">Media</span><span class="p">:</span>
        <span class="n">css</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;all&quot;</span><span class="p">:</span> <span class="p">(</span><span class="s2">&quot;my_styles.css&quot;</span><span class="p">,)</span>
        <span class="p">}</span>
        <span class="n">js</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;my_code.js&quot;</span><span class="p">,)</span>
</pre></div>
</div>
<p><a class="reference internal" href="../staticfiles.html"><span class="doc">staticfiles app</span></a> 将 <a class="reference internal" href="../../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> （如果 <a class="reference internal" href="../../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则 <a class="reference internal" href="../../settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a>）预先加入任何资产路径。同样的规则适用于:ref:<cite>表单上定义的静态资源 &lt;form-asset-paths&gt;</cite>。</p>
<div class="section" id="s-jquery">
<span id="s-contrib-admin-jquery"></span><span id="jquery"></span><span id="contrib-admin-jquery"></span><h4>jQuery<a class="headerlink" href="#jquery" title="永久链接至标题">¶</a></h4>
<p>Django 管理 JavaScript 使用了 <a class="reference external" href="https://jquery.com">jQuery</a> 库。</p>
<p>为了避免与用户提供的脚本或库发生冲突，Django 的 jQuery（3.5.1 版）被命名为 <code class="docutils literal notranslate"><span class="pre">django.jQuery</span></code>。如果你想在自己的管理 JavaScript 中使用 jQuery，而不需要包含第二个副本，你可以在变更列表和添加／删除视图中使用 <code class="docutils literal notranslate"><span class="pre">django.jQuery</span></code> 对象。另外，你自己的管理表单或部件依赖 <code class="docutils literal notranslate"><span class="pre">django.jQuery</span></code> 必须在 <a class="reference internal" href="../../../topics/forms/media.html#assets-as-a-static-definition"><span class="std std-ref">声明表单媒体资产</span></a> 时指定 <code class="docutils literal notranslate"><span class="pre">js=['admin/js/jquery.init.js',</span> <span class="pre">...]</span></code>。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>jQuery 从 3.4.1 版本升级到 3.5.1。</p>
</div>
<p><a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 类默认需要 jQuery，所以除非你有特殊需要，否则不需要将 jQuery 添加到你的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 的媒体资源列表中。例如，如果你要求 jQuery 库在全局命名空间中（例如在使用第三方 jQuery 插件时），或者你需要一个较新版本的 jQuery，你将不得不包含自己的副本。</p>
<p>Django 提供了 jQuery 的非压缩和 “最小化” 版本，分别为 <code class="docutils literal notranslate"><span class="pre">jquery.js</span></code> 和 <code class="docutils literal notranslate"><span class="pre">jquery.min.js</span></code>。</p>
<p><a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 和 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code></a> 有一个 <code class="docutils literal notranslate"><span class="pre">media</span></code> 属性，它返回一个 <code class="docutils literal notranslate"><span class="pre">Media</span></code> 对象的列表，其中存储了表单和／或表单集的 JavaScript 文件的路径。如果 <a class="reference internal" href="../../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">True</span></code>，它将返回各种 JavaScript 文件的未压缩版本，包括 <code class="docutils literal notranslate"><span class="pre">jquery.js</span></code>；如果不是，它将返回 “最小化” 版本。</p>
</div>
</div>
<div class="section" id="s-adding-custom-validation-to-the-admin">
<span id="s-admin-custom-validation"></span><span id="adding-custom-validation-to-the-admin"></span><span id="admin-custom-validation"></span><h3>在管理中添加自定义验证<a class="headerlink" href="#adding-custom-validation-to-the-admin" title="永久链接至标题">¶</a></h3>
<p>你也可以在管理中添加自定义的数据验证。自动管理界面重用 <a class="reference internal" href="../../forms/api.html#module-django.forms" title="django.forms"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.forms</span></code></a>，<code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 类让你能够定义你自己的表单：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">MyArticleAdminForm</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">MyArticleAdminForm</span></code> 可以在任何地方定义，只要你在需要的地方导入。现在，在你的表单中，你可以为任何字段添加自己的自定义验证：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyArticleAdminForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">clean_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># do something that validates your data</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s2">&quot;name&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>在这里使用 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code> 是很重要的，否则可能会出问题。更多信息请参见 <a class="reference internal" href="../../forms/index.html"><span class="doc">表单</span></a> 文档中的 <a class="reference internal" href="../../forms/validation.html"><span class="doc">自定义验证器</span></a> 和 <a class="reference internal" href="../../../topics/forms/modelforms.html#overriding-modelform-clean-method"><span class="std std-ref">验证器返回的模型注解</span></a>。</p>
</div>
</div>
<div class="section" id="s-inlinemodeladmin-objects">
<span id="s-admin-inlines"></span><span id="inlinemodeladmin-objects"></span><span id="admin-inlines"></span><h2><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 对象<a class="headerlink" href="#inlinemodeladmin-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.InlineModelAdmin">
<em class="property">class </em><code class="descname">InlineModelAdmin</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.contrib.admin.TabularInline">
<em class="property">class </em><code class="descname">TabularInline</code><a class="headerlink" href="#django.contrib.admin.TabularInline" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.contrib.admin.StackedInline">
<em class="property">class </em><code class="descname">StackedInline</code><a class="headerlink" href="#django.contrib.admin.StackedInline" title="永久链接至目标">¶</a></dt>
<dd><p>管理界面可以在同一页面上与父模型编辑模型。这些被称为内联。假设你有这两个模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Book</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
   <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Author</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
   <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>你可以在作者页面上编辑作者撰写的书籍。你可以通过在 <code class="docutils literal notranslate"><span class="pre">ModelAdmin.inlines</span></code> 中指定内联来添加到模型中：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">BookInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Book</span>

<span class="k">class</span> <span class="nc">AuthorAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">BookInline</span><span class="p">,</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>Django 提供了两个 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 的子类，它们是：</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.admin.TabularInline" title="django.contrib.admin.TabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">TabularInline</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.StackedInline" title="django.contrib.admin.StackedInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackedInline</span></code></a></li>
</ul>
<p>这两者之间的区别仅仅是用于呈现它们的模板。</p>
</dd></dl>

<div class="section" id="s-inlinemodeladmin-options">
<span id="inlinemodeladmin-options"></span><h3><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 选项<a class="headerlink" href="#inlinemodeladmin-options" title="永久链接至标题">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 与 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 共享许多相同的功能，并增加了一些自己的功能（共享功能实际上是在 <code class="docutils literal notranslate"><span class="pre">BaseModelAdmin</span></code> 超级类中定义的）。共享的功能有：</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.form" title="django.contrib.admin.InlineModelAdmin.form"><code class="xref py py-attr docutils literal notranslate"><span class="pre">form</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.fields" title="django.contrib.admin.ModelAdmin.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_overrides" title="django.contrib.admin.ModelAdmin.formfield_overrides"><code class="xref py py-attr docutils literal notranslate"><span class="pre">formfield_overrides</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.exclude" title="django.contrib.admin.ModelAdmin.exclude"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exclude</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_horizontal" title="django.contrib.admin.ModelAdmin.filter_horizontal"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_horizontal</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.filter_vertical" title="django.contrib.admin.ModelAdmin.filter_vertical"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filter_vertical</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.ordering" title="django.contrib.admin.ModelAdmin.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">prepopulated_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_fieldsets" title="django.contrib.admin.ModelAdmin.get_fieldsets"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_fieldsets()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_queryset" title="django.contrib.admin.ModelAdmin.get_queryset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_queryset()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.radio_fields" title="django.contrib.admin.ModelAdmin.radio_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">radio_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.raw_id_fields" title="django.contrib.admin.InlineModelAdmin.raw_id_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">raw_id_fields</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_choice_field" title="django.contrib.admin.ModelAdmin.formfield_for_choice_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_choice_field()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_foreignkey" title="django.contrib.admin.ModelAdmin.formfield_for_foreignkey"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_foreignkey()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.formfield_for_manytomany" title="django.contrib.admin.ModelAdmin.formfield_for_manytomany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">formfield_for_manytomany()</span></code></a></li>
<li><a class="reference internal" href="#django.contrib.admin.ModelAdmin.has_module_permission" title="django.contrib.admin.ModelAdmin.has_module_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_module_permission()</span></code></a></li>
</ul>
<p><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 类添加或自定义：</p>
<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.model">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">model</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.model" title="永久链接至目标">¶</a></dt>
<dd><p>内联使用的模型。这是必须的。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.fk_name">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">fk_name</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.fk_name" title="永久链接至目标">¶</a></dt>
<dd><p>模型上外键的名称。在大多数情况下会自动处理，但如果同一父模型有多个外键，则必须明确指定 <code class="docutils literal notranslate"><span class="pre">fk_name</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.formset">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">formset</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.formset" title="永久链接至目标">¶</a></dt>
<dd><p>默认为 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.models.BaseInlineFormSet" title="django.forms.models.BaseInlineFormSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseInlineFormSet</span></code></a>。使用你自己的表单集可以给你提供很多定制的可能性。内联是围绕 <a class="reference internal" href="../../../topics/forms/modelforms.html#model-formsets"><span class="std std-ref">模型表单集</span></a> 建立的。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.form">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">form</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.form" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">form</span></code> 的值默认为 <code class="docutils literal notranslate"><span class="pre">ModelForm</span></code>。当为这个内联创建表单集时，会通过 <a class="reference internal" href="../../forms/models.html#django.forms.models.inlineformset_factory" title="django.forms.models.inlineformset_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">inlineformset_factory()</span></code></a> 传递这个值。</p>
</dd></dl>

<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">当为 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 表单编写自定义验证时，要谨慎编写依赖于父模型特征的验证。如果父模型未能验证，它可能会处于不一致的状态，如 <a class="reference internal" href="../../../topics/forms/modelforms.html#validation-on-modelform"><span class="std std-ref">验证 ModelForm</span></a> 中的警告所述。</p>
</div>
<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.classes">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">classes</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.classes" title="永久链接至目标">¶</a></dt>
<dd><p>一个包含额外 CSS 类的列表或元组，应用于为内联渲染的字段集。默认值为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。如同在 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.fieldsets" title="django.contrib.admin.ModelAdmin.fieldsets"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fieldsets</span></code></a> 中配置的类一样，带有 <code class="docutils literal notranslate"><span class="pre">collapse</span></code> 类的内联将被初始折叠，并且它们的标题将有一个小的 “显示” 链接。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.extra">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">extra</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.extra" title="永久链接至目标">¶</a></dt>
<dd><p>控制表单集在初始表格之外显示的额外表单数量，默认为 3 个。更多信息请参见 <a class="reference internal" href="../../../topics/forms/formsets.html"><span class="doc">表单集文档</span></a>。</p>
<p>对于使用支持 JavaScript 的浏览器的用户，提供了一个 “添加另一个” 的链接，以便在 <code class="docutils literal notranslate"><span class="pre">extra</span></code> 参数提供的内联行之外，再添加任意数量的内联行。</p>
<p>如果当前显示的表单数量超过 <code class="docutils literal notranslate"><span class="pre">max_num</span></code>，或者用户没有启用 JavaScript，动态链接将不会出现。</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_extra" title="django.contrib.admin.InlineModelAdmin.get_extra"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_extra()</span></code></a> 还允许你自定义额外表格的数量。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.max_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">max_num</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.max_num" title="永久链接至目标">¶</a></dt>
<dd><p>这控制了内联中显示表单的最大数量。这并不直接与对象的数量相关，但如果该值足够小，则可以。更多信息请参见 <span class="xref std std-ref">model-formets-max-num</span>。</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_max_num" title="django.contrib.admin.InlineModelAdmin.get_max_num"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_max_num()</span></code></a> 还允许你自定义额外表单的最大数量。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.min_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">min_num</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.min_num" title="永久链接至目标">¶</a></dt>
<dd><p>这控制了内联中要显示的表单的最少数量。更多信息请参见 <a class="reference internal" href="../../forms/models.html#django.forms.models.modelformset_factory" title="django.forms.models.modelformset_factory"><code class="xref py py-func docutils literal notranslate"><span class="pre">modelformset_factory()</span></code></a>。</p>
<p><a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.get_min_num" title="django.contrib.admin.InlineModelAdmin.get_min_num"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InlineModelAdmin.get_min_num()</span></code></a> 还允许你自定义显示的最小表单数量。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.raw_id_fields">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">raw_id_fields</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.raw_id_fields" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，Django 的管理员对 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 的字段使用选择框接口（&lt;select&gt;）。有时候，你不想产生必须选择所有相关的实例来显示在下拉框中的开销。</p>
<p><code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> 是你想改变为 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> 或 <code class="docutils literal notranslate"><span class="pre">ManyToManyField</span></code> 的 <code class="docutils literal notranslate"><span class="pre">Input</span></code> 部件的字段列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BookInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Book</span>
    <span class="n">raw_id_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;pages&quot;</span><span class="p">,)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.template">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">template</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.template" title="永久链接至目标">¶</a></dt>
<dd><p>用于在页面上呈现内联的模板。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.verbose_name">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">verbose_name</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.verbose_name" title="永久链接至目标">¶</a></dt>
<dd><p>对模型内部 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 类中的 <code class="docutils literal notranslate"><span class="pre">verbose_name</span></code> 的覆盖。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.verbose_name_plural">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">verbose_name_plural</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.verbose_name_plural" title="永久链接至目标">¶</a></dt>
<dd><p>对模型内部 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 类中的 <code class="docutils literal notranslate"><span class="pre">verbose_name_plural</span></code> 的覆盖。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.can_delete">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">can_delete</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.can_delete" title="永久链接至目标">¶</a></dt>
<dd><p>指定是否可以在内联中删除内联对象。默认为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.InlineModelAdmin.show_change_link">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">show_change_link</code><a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.show_change_link" title="永久链接至目标">¶</a></dt>
<dd><p>指定在管理中可以更改的内联对象是否有更改表单的链接。默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_formset">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_formset</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_formset" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 <a class="reference internal" href="../../../topics/forms/modelforms.html#django.forms.models.BaseInlineFormSet" title="django.forms.models.BaseInlineFormSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseInlineFormSet</span></code></a> 类，用于管理添加／更改视图。<code class="docutils literal notranslate"><span class="pre">obj</span></code> 是被编辑的父对象，或者当添加一个新的父对象时，返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。参见 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_formsets_with_inlines" title="django.contrib.admin.ModelAdmin.get_formsets_with_inlines"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin.get_formsets_with_inlines</span></code></a> 的例子。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_extra">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_extra</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_extra" title="永久链接至目标">¶</a></dt>
<dd><p>返回要使用的额外内联表单的数量。默认情况下，返回 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.extra" title="django.contrib.admin.InlineModelAdmin.extra"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.extra</span></code></a> 属性。</p>
<p>重写此方法，以编程方式确定额外内联表格的数量。例如，可以根据模型实例（作为关键字参数 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 传递）：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BinaryTreeAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">BinaryTree</span>

    <span class="k">def</span> <span class="nf">get_extra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">extra</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">obj</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">extra</span> <span class="o">-</span> <span class="n">obj</span><span class="o">.</span><span class="n">binarytree_set</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">extra</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_max_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_max_num</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_max_num" title="永久链接至目标">¶</a></dt>
<dd><p>返回要使用的额外内联表单的最大数量。默认情况下，返回 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.max_num" title="django.contrib.admin.InlineModelAdmin.max_num"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.max_num</span></code></a> 属性。</p>
<p>重写此方法，以编程方式确定内联表格的最大数量。例如，可以根据模型实例（作为关键字参数 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 传递）：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BinaryTreeAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">BinaryTree</span>

    <span class="k">def</span> <span class="nf">get_max_num</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">max_num</span> <span class="o">=</span> <span class="mi">10</span>
        <span class="k">if</span> <span class="n">obj</span> <span class="ow">and</span> <span class="n">obj</span><span class="o">.</span><span class="n">parent</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">max_num</span> <span class="o">-</span> <span class="mi">5</span>
        <span class="k">return</span> <span class="n">max_num</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.get_min_num">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">get_min_num</code>(<em>request</em>, <em>obj=None</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.get_min_num" title="永久链接至目标">¶</a></dt>
<dd><p>返回要使用的内联表单的最小数量。默认情况下，返回 <a class="reference internal" href="#django.contrib.admin.InlineModelAdmin.min_num" title="django.contrib.admin.InlineModelAdmin.min_num"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InlineModelAdmin.min_num</span></code></a> 属性。</p>
<p>重写此方法，以编程方式确定内联表格的最少数量。例如，这可以基于模型实例（作为关键字参数 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 传递）。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.has_add_permission">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">has_add_permission</code>(<em>request</em>, <em>obj</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.has_add_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许添加内联对象，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。<code class="docutils literal notranslate"><span class="pre">obj</span></code> 是被编辑的父对象，或者当添加一个新的父对象时返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.has_change_permission">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">has_change_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.has_change_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许编辑内联对象，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。<code class="docutils literal notranslate"><span class="pre">obj</span></code> 是被编辑的父对象。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.InlineModelAdmin.has_delete_permission">
<code class="descclassname">InlineModelAdmin.</code><code class="descname">has_delete_permission</code>(<em>request</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.admin.InlineModelAdmin.has_delete_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果允许删除内联对象，应返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。<code class="docutils literal notranslate"><span class="pre">obj</span></code> 是被编辑的父对象。</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">传递给 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 方法的 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 参数是被编辑的父对象，或者在添加新父对象时是 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</div>
</div>
<div class="section" id="s-working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model">
<span id="working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model"></span><h3>与有两个或更多外键到同一父模型的模型一起工作<a class="headerlink" href="#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model" title="永久链接至标题">¶</a></h3>
<p>有时同一个模型可以有多个外键。以这个模型为例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Friendship</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">to_person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;friends&quot;</span><span class="p">)</span>
    <span class="n">from_person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;from_friends&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你想在 <code class="docutils literal notranslate"><span class="pre">Person</span></code> 管理添加／更改页面上显示内联，你需要明确定义外键，因为它不能自动这样做：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Friendship</span>

<span class="k">class</span> <span class="nc">FriendshipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Friendship</span>
    <span class="n">fk_name</span> <span class="o">=</span> <span class="s2">&quot;to_person&quot;</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">FriendshipInline</span><span class="p">,</span>
    <span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-working-with-many-to-many-models">
<span id="working-with-many-to-many-models"></span><h3>与多对多模型一起工作<a class="headerlink" href="#working-with-many-to-many-models" title="永久链接至标题">¶</a></h3>
<p>默认情况下，多对多关系的管理部件将显示在哪个模型上，包含对 <a class="reference internal" href="../../models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 的实际引用。根据你的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 定义，你模型中的每个多对多字段将由一个标准的 HTML <code class="docutils literal notranslate"><span class="pre">&lt;select</span> <span class="pre">multiple&gt;</span></code>，一个水平或垂直过滤器，或一个 <code class="docutils literal notranslate"><span class="pre">raw_id_fields</span></code> 小部件来表示。然而，也可以用内联来代替这些部件。</p>
<p>假设我们有以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;groups&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你想使用内联显示多对多关系，你可以通过为关系定义一个 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 对象来实现：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">MembershipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Group</span><span class="o">.</span><span class="n">members</span><span class="o">.</span><span class="n">through</span>

<span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">MembershipInline</span><span class="p">,</span>
    <span class="p">]</span>

<span class="k">class</span> <span class="nc">GroupAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">MembershipInline</span><span class="p">,</span>
    <span class="p">]</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>在这个例子中，有两个特点值得注意。</p>
<p>首先 <code class="docutils literal notranslate"><span class="pre">MembershipInline</span></code> 类引用 <code class="docutils literal notranslate"><span class="pre">Group.members.through</span></code>。<code class="docutils literal notranslate"><span class="pre">through</span></code> 属性是对管理多对多关系的模型的引用。当你定义一个多对多字段时，这个模型是由 Django 自动创建的。</p>
<p>其次，<code class="docutils literal notranslate"><span class="pre">GroupAdmin</span></code> 必须手动排除 <code class="docutils literal notranslate"><span class="pre">members</span></code> 字段。Django 会在定义关系的模型上显示一个多对多字段的管理部件（在本例中是 <code class="docutils literal notranslate"><span class="pre">Group</span></code>）。如果你想使用一个内联模型来表示多对多关系，你必须告诉 Django 的管理 <em>不要</em> 显示这个部件，否则你将在你的管理页面上有两个部件来管理这个关系。</p>
<p>请注意，当使用这种技术时， <a class="reference internal" href="../../signals.html#django.db.models.signals.m2m_changed" title="django.db.models.signals.m2m_changed"><code class="xref py py-data docutils literal notranslate"><span class="pre">m2m_changed</span></code></a> 信号并没有被触发。这是因为对于管理来说，<code class="docutils literal notranslate"><span class="pre">through</span></code> 只是一个有两个外键字段的模型，而不是一个多对多的关系。</p>
<p>在所有其他方面，<code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 和其他的完全一样。你可以使用任何正常的 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 属性自定义外观。</p>
</div>
<div class="section" id="s-working-with-many-to-many-intermediary-models">
<span id="working-with-many-to-many-intermediary-models"></span><h3>与多对多中间模型一起工作<a class="headerlink" href="#working-with-many-to-many-intermediary-models" title="永久链接至标题">¶</a></h3>
<p>当你使用 <code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField`的``through</span></code> 参数指定一个中介模型时，管理默认不会显示一个部件。这是因为该中介模型的每一个实例所需要的信息比单个部件所能显示的信息要多，而且多个部件所需要的布局会根据中介模型的不同而不同。</p>
<p>然而，我们仍然希望能够在线编辑这些信息。幸运的是，我们可以通过内联管理模型来实现这一点。假设我们有以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="s1">&#39;Membership&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Membership</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Group</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">date_joined</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">invite_reason</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
</pre></div>
</div>
<p>在管理中显示这个中间模型的第一步是为 <code class="docutils literal notranslate"><span class="pre">Membership</span></code> 模型定义一个内联类：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MembershipInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">TabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Membership</span>
    <span class="n">extra</span> <span class="o">=</span> <span class="mi">1</span>
</pre></div>
</div>
<p>这个例子对 <code class="docutils literal notranslate"><span class="pre">Membership</span></code> 模型使用默认的 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 值，并将额外的添加表单限制为一个。这可以使用 <code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 类的任何可用选项来定制。</p>
<p>现在为 <code class="docutils literal notranslate"><span class="pre">Person</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Group</span></code> 模型创建管理视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">PersonAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MembershipInline</span><span class="p">,)</span>

<span class="k">class</span> <span class="nc">GroupAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">MembershipInline</span><span class="p">,)</span>
</pre></div>
</div>
<p>最后，在管理网站上注册你的 <code class="docutils literal notranslate"><span class="pre">Person</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Group</span></code> 模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">PersonAdmin</span><span class="p">)</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Group</span><span class="p">,</span> <span class="n">GroupAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>现在你的管理网站已经设置好了，可以在 <code class="docutils literal notranslate"><span class="pre">Person</span></code> 或 <code class="docutils literal notranslate"><span class="pre">Group</span></code> 的详情页中在线编辑  <code class="docutils literal notranslate"><span class="pre">Membership</span></code> 对象。</p>
</div>
<div class="section" id="s-using-generic-relations-as-an-inline">
<span id="s-id3"></span><span id="using-generic-relations-as-an-inline"></span><span id="id3"></span><h3>使用通用关系作为内联<a class="headerlink" href="#using-generic-relations-as-an-inline" title="永久链接至标题">¶</a></h3>
<p>可以用一个内联与通用相关的对象。假设你有以下模型：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.contenttypes.fields</span> <span class="kn">import</span> <span class="n">GenericForeignKey</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Image</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">image</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ImageField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="s2">&quot;images&quot;</span><span class="p">)</span>
    <span class="n">content_type</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ContentType</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="n">object_id</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">PositiveIntegerField</span><span class="p">()</span>
    <span class="n">content_object</span> <span class="o">=</span> <span class="n">GenericForeignKey</span><span class="p">(</span><span class="s2">&quot;content_type&quot;</span><span class="p">,</span> <span class="s2">&quot;object_id&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Product</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你想允许在 <code class="docutils literal notranslate"><span class="pre">Product``上编辑和创建一个</span> <span class="pre">``Image</span></code> 实例，添加／更改视图，你可以使用由 <a class="reference internal" href="../contenttypes.html#module-django.contrib.contenttypes.admin" title="django.contrib.contenttypes.admin"><code class="xref py py-mod docutils literal notranslate"><span class="pre">admin</span></code></a> 提供的 <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericTabularInline" title="django.contrib.contenttypes.admin.GenericTabularInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericTabularInline</span></code></a> 或 <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericStackedInline" title="django.contrib.contenttypes.admin.GenericStackedInline"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericStackedInline</span></code></a> （都是 <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="django.contrib.contenttypes.admin.GenericInlineModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericInlineModelAdmin</span></code></a> 的子类）。它们分别为代表内联对象的表单实现了表格式和堆栈式的可视化布局，就像它们的非通用对应物一样。它们的行为就像其他内联一样。在你的 <code class="docutils literal notranslate"><span class="pre">admin.py</span></code> 中，对于这个示例应用程序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.admin</span> <span class="kn">import</span> <span class="n">GenericTabularInline</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Image</span><span class="p">,</span> <span class="n">Product</span>

<span class="k">class</span> <span class="nc">ImageInline</span><span class="p">(</span><span class="n">GenericTabularInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Image</span>

<span class="k">class</span> <span class="nc">ProductAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">ImageInline</span><span class="p">,</span>
    <span class="p">]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Product</span><span class="p">,</span> <span class="n">ProductAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>更多具体信息请参见 <a class="reference internal" href="../contenttypes.html"><span class="doc">contenttypes 文档</span></a>。</p>
</div>
</div>
<div class="section" id="s-overriding-admin-templates">
<span id="s-admin-overriding-templates"></span><span id="overriding-admin-templates"></span><span id="admin-overriding-templates"></span><h2>覆盖管理模板<a class="headerlink" href="#overriding-admin-templates" title="永久链接至标题">¶</a></h2>
<p>你可以覆盖许多模板，管理模块用于生成管理网站的各种页面。你甚至可以为特定的应用程序或特定的模型覆盖其中的一些模板。</p>
<div class="section" id="s-set-up-your-projects-admin-template-directories">
<span id="set-up-your-projects-admin-template-directories"></span><h3>设置你的项目管理模板目录<a class="headerlink" href="#set-up-your-projects-admin-template-directories" title="永久链接至标题">¶</a></h3>
<p>管理模板文件位于 <code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin</span></code> 目录下。</p>
<p>为了覆盖其中的一个或多个目录，首先在你的项目的 <code class="docutils literal notranslate"><span class="pre">templates</span></code> 目录下创建一个 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 目录。这个目录可以是你在 <code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code> 后台的 <a class="reference internal" href="../../settings.html#std:setting-TEMPLATES-DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DIRS</span></code></a> 选项中指定的任何一个目录。如果你自定义了 <code class="docutils literal notranslate"><span class="pre">'loaders'</span></code> 选项，请确保 <code class="docutils literal notranslate"><span class="pre">'django.template.loaders.filesystem.Loader'</span></code> 出现在 <code class="docutils literal notranslate"><span class="pre">'django.template.loaders.app_directories.Loader'</span></code> 之前，这样你的自定义模板就会被模板加载系统发现，而不是 <a class="reference internal" href="#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.admin</span></code></a> 所包含的模板。</p>
<p>在这个 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 目录中，创建以你的应用程序命名的子目录。在这些应用程序子目录中，创建以你的模型命名的子目录。注意，管理员应用程序在查找目录时，会将模型名称小写，所以如果你要在大小写敏感的文件系统上运行应用程序，请确保你将目录命名为所有小写。</p>
<p>要覆盖特定应用的管理模板，从 <code class="docutils literal notranslate"><span class="pre">django/contrib/admin/templates/admin</span></code> 目录中复制并编辑模板，并将其保存到你刚刚创建的一个目录中。</p>
<p>例如，如果我们想在名为 <code class="docutils literal notranslate"><span class="pre">my_app</span></code> 的应用程序中的所有模型的变更列表视图中添加一个工具，我们会将 <code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin/change_list.html</span></code> 复制到我们项目的 <code class="docutils literal notranslate"><span class="pre">templates/admin/my_app/</span></code> 目录下，并进行任何必要的更改。</p>
<p>如果我们想只为名为 <code class="docutils literal notranslate"><span class="pre">Page</span></code> 的特定模型在变更列表视图中添加一个工具，我们会将同样的文件复制到项目的 <code class="docutils literal notranslate"><span class="pre">templates/admin/my_app/page</span></code> 目录下。</p>
</div>
<div class="section" id="s-overriding-vs-replacing-an-admin-template">
<span id="overriding-vs-replacing-an-admin-template"></span><h3>覆盖 vs. 替换管理模板<a class="headerlink" href="#overriding-vs-replacing-an-admin-template" title="永久链接至标题">¶</a></h3>
<p>由于管理模板的模块化设计，通常没有必要也不建议更换整个模板。最好是只覆盖你需要更改的模板部分。</p>
<p>继续上面的例子，我们想在 <code class="docutils literal notranslate"><span class="pre">History</span></code> 工具旁边为 <code class="docutils literal notranslate"><span class="pre">Page</span></code> 模型添加一个新的链接。在看了 <code class="docutils literal notranslate"><span class="pre">change_form.html</span></code> 后，我们确定我们只需要覆盖 <code class="docutils literal notranslate"><span class="pre">object-tools-items</span></code> 块。因此这里是我们新的 <code class="docutils literal notranslate"><span class="pre">change_form.html</span></code>。</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">&quot;admin/change_form.html&quot;</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">load</span> <span class="nv">i18n</span> <span class="nv">admin_urls</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">object-tools-items</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;history&#39;</span> <span class="nv">original.pk</span><span class="o">|</span><span class="nf">admin_urlquote</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;historylink&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">translate</span> <span class="s2">&quot;History&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;mylink/&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;historylink&quot;</span><span class="p">&gt;</span>My Link<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">has_absolute_url</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>
            <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;admin:view_on_site&#39;</span> <span class="nv">content_type_id</span> <span class="nv">original.pk</span> <span class="cp">%}</span><span class="s">&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;viewsitelink&quot;</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">translate</span> <span class="s2">&quot;View on site&quot;</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这就是了！如果我们把这个文件放在 <code class="docutils literal notranslate"><span class="pre">templates/admin/my_app</span></code> 目录下，我们的链接就会出现在 my_app 内所有模型的更改表单中。</p>
</div>
<div class="section" id="s-templates-which-may-be-overridden-per-app-or-model">
<span id="s-admin-templates-overridden-per-app-or-model"></span><span id="templates-which-may-be-overridden-per-app-or-model"></span><span id="admin-templates-overridden-per-app-or-model"></span><h3>每个应用或模型可覆盖的模板<a class="headerlink" href="#templates-which-may-be-overridden-per-app-or-model" title="永久链接至标题">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">contrib/admin/templates/admin</span></code> 中的模板并非每个应用程序或每个模型都可以被覆盖。以下情况可以：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">actions.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">app_index.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_form.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_form_object_tools.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_list.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_list_object_tools.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">change_list_results.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">date_hierarchy.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">delete_confirmation.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">object_history.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">pagination.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">popup_response.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">prepopulated_fields_js.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">search_form.html</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">submit_line.html</span></code></li>
</ul>
<p>对于那些不能以这种方式覆盖的模板，你仍然可以通过将新版本放在你的 <code class="docutils literal notranslate"><span class="pre">templates/admin</span></code> 目录下，为整个项目覆盖它们。这对于创建自定义 404 和 500 页面特别有用。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">一些管理模板，如 <code class="docutils literal notranslate"><span class="pre">change_list_results.html</span></code> 是用来呈现自定义包含标签的。这些可以被覆盖，但在这种情况下，你可能最好创建你自己版本的标签，并给它一个不同的名字。这样你就可以有选择地使用它。</p>
</div>
</div>
<div class="section" id="s-root-and-login-templates">
<span id="root-and-login-templates"></span><h3>根和登录模板<a class="headerlink" href="#root-and-login-templates" title="永久链接至标题">¶</a></h3>
<p>如果你想更改索引、登录或注销模板，你最好创建自己的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例（见下文），并更改 <a class="reference internal" href="#django.contrib.admin.AdminSite.index_template" title="django.contrib.admin.AdminSite.index_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.index_template</span></code></a> 、<a class="reference internal" href="#django.contrib.admin.AdminSite.login_template" title="django.contrib.admin.AdminSite.login_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.login_template</span></code></a> 或 <a class="reference internal" href="#django.contrib.admin.AdminSite.logout_template" title="django.contrib.admin.AdminSite.logout_template"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.logout_template</span></code></a> 属性。</p>
</div>
</div>
<div class="section" id="s-theming-support">
<span id="s-admin-theming"></span><span id="theming-support"></span><span id="admin-theming"></span><h2>主题化支持<a class="headerlink" href="#theming-support" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>管理使用 CSS 变量来定义颜色。这允许改变颜色，而不必覆盖许多单独的 CSS 规则。例如，如果你喜欢紫色而不是蓝色，你可以添加一个 <code class="docutils literal notranslate"><span class="pre">admin/base.html</span></code> 模板覆盖到你的项目中。</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s1">&#39;admin/base.html&#39;</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">block</span> <span class="nv">extrastyle</span> <span class="cp">%}{{</span> <span class="nb">block</span><span class="nv">.super</span> <span class="cp">}}</span>
<span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span>
<span class="p">:</span><span class="nd">root</span> <span class="p">{</span>
  <span class="nv">--primary</span><span class="p">:</span> <span class="mh">#9774d5</span><span class="p">;</span>
  <span class="nv">--secondary</span><span class="p">:</span> <span class="mh">#785cab</span><span class="p">;</span>
  <span class="nv">--link-fg</span><span class="p">:</span> <span class="mh">#7c449b</span><span class="p">;</span>
  <span class="nv">--link-selected-fg</span><span class="p">:</span> <span class="mh">#8f5bb2</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>定义一个深色主题，并根据 <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/CSS/&#64;media/prefers-color-scheme">prefers-color-scheme</a> 媒体查询应用。</p>
<p>CSS 变量列表定义在 <code class="file docutils literal notranslate"><span class="pre">django/contrib/admin/static/admin/css/base.css</span></code>。</p>
</div>
<div class="section" id="s-adminsite-objects">
<span id="adminsite-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 对象<a class="headerlink" href="#adminsite-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.AdminSite">
<em class="property">class </em><code class="descname">AdminSite</code>(<em>name='admin'</em>)<a class="headerlink" href="#django.contrib.admin.AdminSite" title="永久链接至目标">¶</a></dt>
<dd><p>一个 Django 管理站点由 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.sites.AdminSite</span></code> 的实例来表示；默认情况下，这个类的实例被创建为 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.site</span></code>，你可以用它来注册你的模型和 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 实例。</p>
<p>如果你想自定义默认的管理站点，你可以 <a class="reference internal" href="#overriding-default-admin-site"><span class="std std-ref">覆盖它</span></a>。</p>
<p>当构造一个 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 的实例时，你可以使用构造函数的 <code class="docutils literal notranslate"><span class="pre">name</span></code> 参数提供一个唯一的实例名称。这个实例名是用来标识实例的，特别是在 <a class="reference internal" href="#admin-reverse-urls"><span class="std std-ref">反查管理 URL</span></a> 时。如果没有提供实例名，将使用默认的实例名 <code class="docutils literal notranslate"><span class="pre">admin</span></code>。参见 <a class="reference internal" href="#customizing-adminsite"><span class="std std-ref">自定义 AdminSite 类</span></a>，了解定制 <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> 类的例子。</p>
</dd></dl>

<div class="section" id="s-adminsite-attributes">
<span id="adminsite-attributes"></span><h3><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 属性<a class="headerlink" href="#adminsite-attributes" title="永久链接至标题">¶</a></h3>
<p>模板可以覆盖或扩展基本的管理模板，如 <a class="reference internal" href="#admin-overriding-templates"><span class="std std-ref">覆盖管理模板</span></a> 中所述。</p>
<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_header">
<code class="descclassname">AdminSite.</code><code class="descname">site_header</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_header" title="永久链接至目标">¶</a></dt>
<dd><p>要放在每个管理页面顶部的文字，作为 <code class="docutils literal notranslate"><span class="pre">&lt;h1&gt;</span></code> （一个字符串）。默认情况下，是 “Django 管理”。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_title">
<code class="descclassname">AdminSite.</code><code class="descname">site_title</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_title" title="永久链接至目标">¶</a></dt>
<dd><p>在每个管理页面的 <code class="docutils literal notranslate"><span class="pre">&lt;title&gt;</span></code> （字符串）末尾放置的文字。默认情况下，是 “Django 站点管理”。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.site_url">
<code class="descclassname">AdminSite.</code><code class="descname">site_url</code><a class="headerlink" href="#django.contrib.admin.AdminSite.site_url" title="永久链接至目标">¶</a></dt>
<dd><p>每个管理页面顶部的 “查看网站” 链接的 URL。默认情况下，<code class="docutils literal notranslate"><span class="pre">site_url</span></code> 是 <code class="docutils literal notranslate"><span class="pre">/</span></code>。将其设置为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 以删除该链接。</p>
<p>对于运行在子路径上的站点， <a class="reference internal" href="#django.contrib.admin.AdminSite.each_context" title="django.contrib.admin.AdminSite.each_context"><code class="xref py py-meth docutils literal notranslate"><span class="pre">each_context()</span></code></a> 方法会检查当前请求是否设置了 <code class="docutils literal notranslate"><span class="pre">request.META['SCRIPT_NAME']</span></code>，如果 <code class="docutils literal notranslate"><span class="pre">site_url</span></code> 没有设置为 <code class="docutils literal notranslate"><span class="pre">/</span></code> 以外的内容，则使用该值。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.index_title">
<code class="descclassname">AdminSite.</code><code class="descname">index_title</code><a class="headerlink" href="#django.contrib.admin.AdminSite.index_title" title="永久链接至目标">¶</a></dt>
<dd><p>放在管理索引页顶部的文字（一个字符串）。默认情况下，是 “网站管理”。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.index_template">
<code class="descclassname">AdminSite.</code><code class="descname">index_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.index_template" title="永久链接至目标">¶</a></dt>
<dd><p>管理网站主索引视图将使用的自定义模板的路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.app_index_template">
<code class="descclassname">AdminSite.</code><code class="descname">app_index_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.app_index_template" title="永久链接至目标">¶</a></dt>
<dd><p>管理网站应用索引视图将使用的自定义模板的路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.empty_value_display">
<code class="descclassname">AdminSite.</code><code class="descname">empty_value_display</code><a class="headerlink" href="#django.contrib.admin.AdminSite.empty_value_display" title="永久链接至目标">¶</a></dt>
<dd><p>用于在管理站点的更改列表中显示空值的字符串。默认值为破折号。该值也可以在每个 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 的基础上被覆盖，也可以在 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 内的自定义字段上设置 <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> 属性。参见 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.empty_value_display" title="django.contrib.admin.ModelAdmin.empty_value_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.empty_value_display</span></code></a> 的例子。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.enable_nav_sidebar">
<code class="descclassname">AdminSite.</code><code class="descname">enable_nav_sidebar</code><a class="headerlink" href="#django.contrib.admin.AdminSite.enable_nav_sidebar" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.1.</span> </div>
<p>一个布尔值，决定是否在大屏幕上显示导航侧栏。默认情况下，它被设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.final_catch_all_view">
<code class="descclassname">AdminSite.</code><code class="descname">final_catch_all_view</code><a class="headerlink" href="#django.contrib.admin.AdminSite.final_catch_all_view" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>一个布尔值，用于决定是否在管理员中添加一个最终的总括视图，将未认证的用户重定向到登录页面。默认情况下，它被设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">不建议将此设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，因为该视图可以保护潜在的模型枚举隐私问题。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.login_template">
<code class="descclassname">AdminSite.</code><code class="descname">login_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.login_template" title="永久链接至目标">¶</a></dt>
<dd><p>管理网站登录视图将使用的自定义模板的路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.login_form">
<code class="descclassname">AdminSite.</code><code class="descname">login_form</code><a class="headerlink" href="#django.contrib.admin.AdminSite.login_form" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="../../../topics/auth/default.html#django.contrib.auth.forms.AuthenticationForm" title="django.contrib.auth.forms.AuthenticationForm"><code class="xref py py-class docutils literal notranslate"><span class="pre">AuthenticationForm</span></code></a> 的子类，将被管理网站登录视图使用。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.logout_template">
<code class="descclassname">AdminSite.</code><code class="descname">logout_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.logout_template" title="永久链接至目标">¶</a></dt>
<dd><p>管理网站注销视图将使用的自定义模板的路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.password_change_template">
<code class="descclassname">AdminSite.</code><code class="descname">password_change_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.password_change_template" title="永久链接至目标">¶</a></dt>
<dd><p>管理网站密码修改视图将使用的自定义模板的路径。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.AdminSite.password_change_done_template">
<code class="descclassname">AdminSite.</code><code class="descname">password_change_done_template</code><a class="headerlink" href="#django.contrib.admin.AdminSite.password_change_done_template" title="永久链接至目标">¶</a></dt>
<dd><p>自定义模板的路径，该模板将被管理员网站密码修改完成后的视图使用。</p>
</dd></dl>

</div>
<div class="section" id="s-adminsite-methods">
<span id="adminsite-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 方法<a class="headerlink" href="#adminsite-methods" title="永久链接至标题">¶</a></h3>
<dl class="method">
<dt id="django.contrib.admin.AdminSite.each_context">
<code class="descclassname">AdminSite.</code><code class="descname">each_context</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.AdminSite.each_context" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个变量字典，将其放入管理站点中每个页面的模板上下文中。</p>
<p>默认情况下包括以下变量和值：</p>
<ul>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_header</span></code> ： <a class="reference internal" href="#django.contrib.admin.AdminSite.site_header" title="django.contrib.admin.AdminSite.site_header"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_header</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_title</span></code> ：<a class="reference internal" href="#django.contrib.admin.AdminSite.site_title" title="django.contrib.admin.AdminSite.site_title"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_title</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">site_url</span></code> ：<a class="reference internal" href="#django.contrib.admin.AdminSite.site_url" title="django.contrib.admin.AdminSite.site_url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AdminSite.site_url</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">has_permission</span></code> ：<a class="reference internal" href="#django.contrib.admin.AdminSite.has_permission" title="django.contrib.admin.AdminSite.has_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AdminSite.has_permission()</span></code></a></p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">available_apps</span></code> ：当前用户可用的 <span class="xref std std-doc">application registry</span> 中的应用程序列表。列表中的每个条目都是一个代表应用程序的字典，其键如下：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">app_label</span></code> ：应用程序标签</li>
<li><code class="docutils literal notranslate"><span class="pre">app_url</span></code> ：管理中应用程序索引的 URL</li>
<li><code class="docutils literal notranslate"><span class="pre">has_module_perms</span></code> ：一个布尔值，表示是否允许当前用户显示和访问模块的索引页</li>
<li><code class="docutils literal notranslate"><span class="pre">models</span></code> ：应用程序中可用模型的清单</li>
</ul>
<p>每个模型都是一个带有以下键的字典</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">object_name</span></code> ：模型类名</li>
<li><code class="docutils literal notranslate"><span class="pre">name</span></code> ：模型的复数名</li>
<li><code class="docutils literal notranslate"><span class="pre">perms</span></code> ：<code class="docutils literal notranslate"><span class="pre">dict</span></code> 跟踪 <code class="docutils literal notranslate"><span class="pre">add</span></code>、<code class="docutils literal notranslate"><span class="pre">change</span></code>、<code class="docutils literal notranslate"><span class="pre">delete</span></code> 和 <code class="docutils literal notranslate"><span class="pre">view</span></code> 权限</li>
<li><code class="docutils literal notranslate"><span class="pre">admin_url</span></code> ：模型的管理变更表 URL</li>
<li><code class="docutils literal notranslate"><span class="pre">add_url</span></code> ：添加新模型实例的管理网址</li>
</ul>
</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.AdminSite.has_permission">
<code class="descclassname">AdminSite.</code><code class="descname">has_permission</code>(<em>request</em>)<a class="headerlink" href="#django.contrib.admin.AdminSite.has_permission" title="永久链接至目标">¶</a></dt>
<dd><p>如果给定的 <code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> 的用户有权限在管理站点中查看至少一个页面，则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。默认要求 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_active" title="django.contrib.auth.models.User.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">User.is_active</span></code></a> 和 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_staff" title="django.contrib.auth.models.User.is_staff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">User.is_staff</span></code></a> 都是 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.AdminSite.register">
<code class="descclassname">AdminSite.</code><code class="descname">register</code>(<em>model_or_iterable</em>, <em>admin_class=None</em>, <em>**options</em>)<a class="headerlink" href="#django.contrib.admin.AdminSite.register" title="永久链接至目标">¶</a></dt>
<dd><p>用给定的 <code class="docutils literal notranslate"><span class="pre">admin_class</span></code> 注册给定的模型类（或类的迭代）。<code class="docutils literal notranslate"><span class="pre">admin_class</span></code> 默认为 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> （默认的管理选项）。如果给定了关键字参数 —— 例如 <code class="docutils literal notranslate"><span class="pre">list_display</span></code> —— 它们将作为选项应用到管理类中。</p>
<p>如果一个模型是抽象的，会引发 <a class="reference internal" href="../../exceptions.html#django.core.exceptions.ImproperlyConfigured" title="django.core.exceptions.ImproperlyConfigured"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImproperlyConfigured</span></code></a>，如果一个模型已经注册，会引发 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.sites.AlreadyRegistered</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.AdminSite.unregister">
<code class="descclassname">AdminSite.</code><code class="descname">unregister</code>(<em>model_or_iterable</em>)<a class="headerlink" href="#django.contrib.admin.AdminSite.unregister" title="永久链接至目标">¶</a></dt>
<dd><p>取消注册给定的模型类（或类的可迭代对象）</p>
<p>如果一个模型还没有注册，就会引发 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.sites.NotRegistered</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-hooking-adminsite-instances-into-your-urlconf">
<span id="s-hooking-adminsite-to-urlconf"></span><span id="hooking-adminsite-instances-into-your-urlconf"></span><span id="hooking-adminsite-to-urlconf"></span><h3>将 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例挂到你的 URLconf 中<a class="headerlink" href="#hooking-adminsite-instances-into-your-urlconf" title="永久链接至标题">¶</a></h3>
<p>设置 Django 管理的最后一步是将你的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例挂到你的 URLconf 中。通过将一个给定的 URL 指向 <code class="docutils literal notranslate"><span class="pre">AdminSite.urls</span></code> 方法来实现。不需要使用 <a class="reference internal" href="../../urls.html#django.urls.include" title="django.urls.include"><code class="xref py py-func docutils literal notranslate"><span class="pre">include()</span></code></a>。</p>
<p>在这个例子中，我们将默认的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.site</span></code> 注册到 URL <code class="docutils literal notranslate"><span class="pre">/admin/</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;admin/&#39;</span><span class="p">,</span> <span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-customizing-the-adminsite-class">
<span id="s-customizing-adminsite"></span><span id="customizing-the-adminsite-class"></span><span id="customizing-adminsite"></span><h3>自定义 <code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code> 类<a class="headerlink" href="#customizing-the-adminsite-class" title="永久链接至标题">¶</a></h3>
<p>如果你想用自定义行为设置你自己的管理站点，你可以自由地将 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 子类化，并覆盖或添加任何你喜欢的内容。然后，创建一个你的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 子类的实例（与你实例化任何其他 Python 类的方式相同），并用它注册你的模型和 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 子类，而不是用默认站点。最后，更新 <code class="file docutils literal notranslate"><span class="pre">myproject/urls.py</span></code> 来引用你的 <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> 子类。</p>
<div class="literal-block-wrapper docutils container" id="id4">
<div class="code-block-caption"><span class="caption-text">myapp/admin.py</span><a class="headerlink" href="#id4" title="永久链接至代码">¶</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin</span> <span class="kn">import</span> <span class="n">AdminSite</span>

<span class="kn">from</span> <span class="nn">.models</span> <span class="kn">import</span> <span class="n">MyModel</span>

<span class="k">class</span> <span class="nc">MyAdminSite</span><span class="p">(</span><span class="n">AdminSite</span><span class="p">):</span>
    <span class="n">site_header</span> <span class="o">=</span> <span class="s1">&#39;Monty Python administration&#39;</span>

<span class="n">admin_site</span> <span class="o">=</span> <span class="n">MyAdminSite</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;myadmin&#39;</span><span class="p">)</span>
<span class="n">admin_site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">MyModel</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="literal-block-wrapper docutils container" id="id5">
<div class="code-block-caption"><span class="caption-text">myproject/urls.py</span><a class="headerlink" href="#id5" title="永久链接至代码">¶</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.admin</span> <span class="kn">import</span> <span class="n">admin_site</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;myadmin/&#39;</span><span class="p">,</span> <span class="n">admin_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<p>请注意，当你使用自己的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例时，你可能不希望自动发现 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 模块，因为你可能会在你的 <code class="docutils literal notranslate"><span class="pre">myproject.admin</span></code> 模块中导入所有应用的 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 模块。这意味着你需要在你的 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中加入 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin.apps.SimpleAdminConfig'</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">'django.contrib.admin'</span></code>。</p>
</div>
<div class="section" id="s-overriding-the-default-admin-site">
<span id="s-overriding-default-admin-site"></span><span id="overriding-the-default-admin-site"></span><span id="overriding-default-admin-site"></span><h3>覆盖默认的管理站点<a class="headerlink" href="#overriding-the-default-admin-site" title="永久链接至标题">¶</a></h3>
<p>你可以通过设置自定义 <code class="docutils literal notranslate"><span class="pre">AppConfig</span></code> 的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">default_site`</span></code> 属性来覆盖默认的 <code class="docutils literal notranslate"><span class="pre">django.contrib.admin.site</span></code>，将其设置为一个 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 子类或一个返回站点实例的可调用的点分隔导入路径。</p>
<div class="literal-block-wrapper docutils container" id="id6">
<div class="code-block-caption"><span class="caption-text">myproject/admin.py</span><a class="headerlink" href="#id6" title="永久链接至代码">¶</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>

<span class="k">class</span> <span class="nc">MyAdminSite</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">AdminSite</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
</div>
<div class="literal-block-wrapper docutils container" id="id7">
<div class="code-block-caption"><span class="caption-text">myproject/apps.py</span><a class="headerlink" href="#id7" title="永久链接至代码">¶</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin.apps</span> <span class="kn">import</span> <span class="n">AdminConfig</span>

<span class="k">class</span> <span class="nc">MyAdminConfig</span><span class="p">(</span><span class="n">AdminConfig</span><span class="p">):</span>
    <span class="n">default_site</span> <span class="o">=</span> <span class="s1">&#39;myproject.admin.MyAdminSite&#39;</span>
</pre></div>
</div>
</div>
<div class="literal-block-wrapper docutils container" id="id8">
<div class="code-block-caption"><span class="caption-text">myproject/settings.py</span><a class="headerlink" href="#id8" title="永久链接至代码">¶</a></div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">[</span>
    <span class="o">...</span>
    <span class="s1">&#39;myproject.apps.MyAdminConfig&#39;</span><span class="p">,</span>  <span class="c1"># replaces &#39;django.contrib.admin&#39;</span>
    <span class="o">...</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-multiple-admin-sites-in-the-same-urlconf">
<span id="s-multiple-admin-sites"></span><span id="multiple-admin-sites-in-the-same-urlconf"></span><span id="multiple-admin-sites"></span><h3>同一个 URLconf 中的多个管理站点<a class="headerlink" href="#multiple-admin-sites-in-the-same-urlconf" title="永久链接至标题">¶</a></h3>
<p>你可以在同一个 Django 驱动的网站上创建多个管理站点的实例。创建多个 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 的实例，并将每个实例放置在不同的 URL 中。</p>
<p>在这个例子中，URL <code class="docutils literal notranslate"><span class="pre">/basic-admin/</span></code> 和 <code class="docutils literal notranslate"><span class="pre">/advanced-admin/</span></code> 分别使用 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例 <code class="docutils literal notranslate"><span class="pre">myproject.admin.basic_site</span></code> 和 <code class="docutils literal notranslate"><span class="pre">myproject.admin.advanced_site</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">myproject.admin</span> <span class="kn">import</span> <span class="n">advanced_site</span><span class="p">,</span> <span class="n">basic_site</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;basic-admin/&#39;</span><span class="p">,</span> <span class="n">basic_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;advanced-admin/&#39;</span><span class="p">,</span> <span class="n">advanced_site</span><span class="o">.</span><span class="n">urls</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例的构造函数只有一个参数，即名称，可以是任何你喜欢的名称。这个参数成为 URL 名称的前缀，以便 <a class="reference internal" href="#admin-reverse-urls"><span class="std std-ref">反查它们</span></a>。只有当你使用一个以上的 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 时才需要这样做。</p>
</div>
<div class="section" id="s-adding-views-to-admin-sites">
<span id="adding-views-to-admin-sites"></span><h3>在管理站点上添加视图<a class="headerlink" href="#adding-views-to-admin-sites" title="永久链接至标题">¶</a></h3>
<p>就像 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 一样， <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> 也提供了一个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_urls" title="django.contrib.admin.ModelAdmin.get_urls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_urls()</span></code></a> 方法，它可以被重写来定义站点的其他视图。要添加一个新的视图到你的管理站点，扩展基本的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_urls" title="django.contrib.admin.ModelAdmin.get_urls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_urls()</span></code></a> 方法来包含一个新视图的模式。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">任何使用管理模板或扩展基本管理模板的视图，都应该在渲染模板之前设置 <code class="docutils literal notranslate"><span class="pre">request.current_app</span></code>。如果你的视图是在 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 上，它应该设置为 <code class="docutils literal notranslate"><span class="pre">self.name</span></code>；如果你的视图是在 <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 上，它应该设置为 <code class="docutils literal notranslate"><span class="pre">self.admin_site.name</span></code>。</p>
</div>
</div>
<div class="section" id="s-adding-a-password-reset-feature">
<span id="s-auth-password-reset"></span><span id="adding-a-password-reset-feature"></span><span id="auth-password-reset"></span><h3>增加密码重置功能<a class="headerlink" href="#adding-a-password-reset-feature" title="永久链接至标题">¶</a></h3>
<p>你可以在你的 URLconf 中添加几行字，就可以给管理网站添加密码重置功能。具体来说，添加以下四种模式：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">auth_views</span>

<span class="n">path</span><span class="p">(</span>
    <span class="s1">&#39;admin/password_reset/&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;admin_password_reset&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">path</span><span class="p">(</span>
    <span class="s1">&#39;admin/password_reset/done/&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetDoneView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_done&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">path</span><span class="p">(</span>
    <span class="s1">&#39;reset/&lt;uidb64&gt;/&lt;token&gt;/&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetConfirmView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_confirm&#39;</span><span class="p">,</span>
<span class="p">),</span>
<span class="n">path</span><span class="p">(</span>
    <span class="s1">&#39;reset/done/&#39;</span><span class="p">,</span>
    <span class="n">auth_views</span><span class="o">.</span><span class="n">PasswordResetCompleteView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;password_reset_complete&#39;</span><span class="p">,</span>
<span class="p">),</span>
</pre></div>
</div>
<p>（这假定你已经在 <code class="docutils literal notranslate"><span class="pre">admin/</span></code> 添加了管理员，并且要求你把以 <code class="docutils literal notranslate"><span class="pre">^admin/</span></code> 为开头的 URL 放在包含管理程序本身的行之前）。</p>
<p><code class="docutils literal notranslate"><span class="pre">admin_password_reset</span></code> 命名的 URL 的存在，会使默认的管理登录页面的密码框下出现 “忘记密码了？” 的链接。</p>
</div>
</div>
<div class="section" id="s-logentry-objects">
<span id="logentry-objects"></span><h2><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 对象<a class="headerlink" href="#logentry-objects" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.admin.models.LogEntry">
<em class="property">class </em><code class="descclassname">models.</code><code class="descname">LogEntry</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 类可以跟踪通过管理界面完成的对象的添加、更改和删除。</p>
</dd></dl>

<div class="section" id="s-logentry-attributes">
<span id="logentry-attributes"></span><h3><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 属性<a class="headerlink" href="#logentry-attributes" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.action_time">
<code class="descclassname">LogEntry.</code><code class="descname">action_time</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.action_time" title="永久链接至目标">¶</a></dt>
<dd><p>动作的日期和时间。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.user">
<code class="descclassname">LogEntry.</code><code class="descname">user</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.user" title="永久链接至目标">¶</a></dt>
<dd><p>执行操作的用户（一个 <a class="reference internal" href="../../settings.html#std:setting-AUTH_USER_MODEL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">AUTH_USER_MODEL</span></code></a> 实例）。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.content_type">
<code class="descclassname">LogEntry.</code><code class="descname">content_type</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.content_type" title="永久链接至目标">¶</a></dt>
<dd><p>修改对象的 <a class="reference internal" href="../contenttypes.html#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContentType</span></code></a> 。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.object_id">
<code class="descclassname">LogEntry.</code><code class="descname">object_id</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.object_id" title="永久链接至目标">¶</a></dt>
<dd><p>修改对象主键的文字表示。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.object_repr">
<code class="descclassname">LogEntry.</code><code class="descname">object_repr</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.object_repr" title="永久链接至目标">¶</a></dt>
<dd><p>修改后的对象 <code class="docutils literal notranslate"><span class="pre">repr()</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.action_flag">
<code class="descclassname">LogEntry.</code><code class="descname">action_flag</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.action_flag" title="永久链接至目标">¶</a></dt>
<dd><p>记录的动作类型： <code class="docutils literal notranslate"><span class="pre">ADDITION</span></code>、<code class="docutils literal notranslate"><span class="pre">CHANGE</span></code>、<code class="docutils literal notranslate"><span class="pre">DELETION</span></code>。</p>
<p>例如，要获取所有通过管理完成的添加列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin.models</span> <span class="kn">import</span> <span class="n">ADDITION</span><span class="p">,</span> <span class="n">LogEntry</span>

<span class="n">LogEntry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">action_flag</span><span class="o">=</span><span class="n">ADDITION</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.admin.models.LogEntry.change_message">
<code class="descclassname">LogEntry.</code><code class="descname">change_message</code><a class="headerlink" href="#django.contrib.admin.models.LogEntry.change_message" title="永久链接至目标">¶</a></dt>
<dd><p>对修改的详细描述。例如，在编辑的情况下，消息中包含了被编辑的字段列表。Django 管理网站将这些内容格式化为 JSON 结构，这样 <a class="reference internal" href="#django.contrib.admin.models.LogEntry.get_change_message" title="django.contrib.admin.models.LogEntry.get_change_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_change_message()</span></code></a> 就可以重新组成一个用当前用户语言翻译的消息。不过自定义代码可能会将其设置为纯字符串。建议你使用 <a class="reference internal" href="#django.contrib.admin.models.LogEntry.get_change_message" title="django.contrib.admin.models.LogEntry.get_change_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_change_message()</span></code></a> 方法来检索这个值，而不是直接访问它。</p>
</dd></dl>

</div>
<div class="section" id="s-logentry-methods">
<span id="logentry-methods"></span><h3><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 方法<a class="headerlink" href="#logentry-methods" title="永久链接至标题">¶</a></h3>
<dl class="method">
<dt id="django.contrib.admin.models.LogEntry.get_edited_object">
<code class="descclassname">LogEntry.</code><code class="descname">get_edited_object</code>()<a class="headerlink" href="#django.contrib.admin.models.LogEntry.get_edited_object" title="永久链接至目标">¶</a></dt>
<dd><p>返回被引用对象的快捷方式。</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.admin.models.LogEntry.get_change_message">
<code class="descclassname">LogEntry.</code><code class="descname">get_change_message</code>()<a class="headerlink" href="#django.contrib.admin.models.LogEntry.get_change_message" title="永久链接至目标">¶</a></dt>
<dd><p>将 <a class="reference internal" href="#django.contrib.admin.models.LogEntry.change_message" title="django.contrib.admin.models.LogEntry.change_message"><code class="xref py py-attr docutils literal notranslate"><span class="pre">change_message</span></code></a> 格式化并翻译成当前用户语言。在 Django 1.10 之前创建的消息将始终以其登录时的语言显示。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-reversing-admin-urls">
<span id="s-admin-reverse-urls"></span><span id="reversing-admin-urls"></span><span id="admin-reverse-urls"></span><h2>反查管理 URL<a class="headerlink" href="#reversing-admin-urls" title="永久链接至标题">¶</a></h2>
<p>当部署了一个 <a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> 时，可以使用 Django 的 <a class="reference internal" href="../../../topics/http/urls.html#naming-url-patterns"><span class="std std-ref">URL 反查系统</span></a> 访问该网站提供的视图。</p>
<p><a class="reference internal" href="#django.contrib.admin.AdminSite" title="django.contrib.admin.AdminSite"><code class="xref py py-class docutils literal notranslate"><span class="pre">AdminSite</span></code></a> 提供了以下命名的 URL 模式。</p>
<table class="docutils">
<colgroup>
<col width="30%" />
<col width="29%" />
<col width="41%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">页面</th>
<th class="head">URL 名称</th>
<th class="head">参数</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>索引</td>
<td><code class="docutils literal notranslate"><span class="pre">index</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>登录</td>
<td><code class="docutils literal notranslate"><span class="pre">login</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>登出</td>
<td><code class="docutils literal notranslate"><span class="pre">logout</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>密码更改</td>
<td><code class="docutils literal notranslate"><span class="pre">password_change</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>密码更改完成</td>
<td><code class="docutils literal notranslate"><span class="pre">password_change_done</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>i18n JavaScript</td>
<td><code class="docutils literal notranslate"><span class="pre">jsi18n</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>应用索引页面</td>
<td><code class="docutils literal notranslate"><span class="pre">app_list</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">app_label</span></code></td>
</tr>
<tr class="row-odd"><td>重定向到对象的页面</td>
<td><code class="docutils literal notranslate"><span class="pre">view_on_site</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">content_type_id</span></code>, <code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
</tbody>
</table>
<p>每个 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 实例都提供一组额外的命名 URL：</p>
<table class="docutils">
<colgroup>
<col width="27%" />
<col width="57%" />
<col width="16%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">页面</th>
<th class="head">URL 名称</th>
<th class="head">参数</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>变更列表</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_changelist</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>增加</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_add</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>历史</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_history</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
<tr class="row-odd"><td>删除</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_delete</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
<tr class="row-even"><td>修改</td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">app_label</span> <span class="pre">}}_{{</span> <span class="pre">model_name</span> <span class="pre">}}_change</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">object_id</span></code></td>
</tr>
</tbody>
</table>
<p><code class="docutils literal notranslate"><span class="pre">UserAdmin</span></code> 提供一个命名的 URL：</p>
<table class="docutils">
<colgroup>
<col width="27%" />
<col width="57%" />
<col width="16%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">页面</th>
<th class="head">URL 名称</th>
<th class="head">参数</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>密码更改</td>
<td><code class="docutils literal notranslate"><span class="pre">auth_user_password_change</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">user_id</span></code></td>
</tr>
</tbody>
</table>
<p>这些命名的 URL 在应用程序命名空间 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 和与 Site 实例名称相对应的实例命名空间中注册。</p>
<p>所以 —— 如果你想在默认的管理员中获得一个特定的 <code class="docutils literal notranslate"><span class="pre">Choice</span></code> 对象（来自民意调查应用程序）的变更视图的引用，你会调用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Choice</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">change_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:polls_choice_change&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">,))</span>
</pre></div>
</div>
<p>这将找到管理应用程序的第一个注册实例（无论实例名称如何），并解析到该实例中改变 <code class="docutils literal notranslate"><span class="pre">poll.Choice</span></code> 实例的视图。</p>
<p>如果你想在一个特定的管理实例中找到一个 URL，提供该实例的名称作为反向调用的 <code class="docutils literal notranslate"><span class="pre">current_app</span></code> 提示。例如，如果你特别想从名为 <code class="docutils literal notranslate"><span class="pre">custom</span></code> 的管理实例中找到管理视图，你需要调用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">change_url</span> <span class="o">=</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;admin:polls_choice_change&#39;</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">,),</span> <span class="n">current_app</span><span class="o">=</span><span class="s1">&#39;custom&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>更多细节，请参见 <a class="reference internal" href="../../../topics/http/urls.html#topics-http-reversing-url-namespaces"><span class="std std-ref">反查命名空间的 URL</span></a> 的文档。</p>
<p>为了让模板中的管理网址更容易反查，Django 提供了一个 <code class="docutils literal notranslate"><span class="pre">admin_urlname</span></code> 过滤器，它的参数是一个动作：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">admin_urls</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;add&#39;</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>Add user<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;</span><span class="cp">{%</span> <span class="k">url</span> <span class="nv">opts</span><span class="o">|</span><span class="nf">admin_urlname</span><span class="s1">:&#39;delete&#39;</span> <span class="nv">user.pk</span> <span class="cp">%}</span><span class="s">&quot;</span><span class="p">&gt;</span>Delete this user<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>上面例子中的操作与上面描述的 <a class="reference internal" href="#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> 实例的 URL 名称的最后一部分相匹配。<code class="docutils literal notranslate"><span class="pre">opts</span></code> 变量可以是任何具有 <code class="docutils literal notranslate"><span class="pre">app_label</span></code> 和 <code class="docutils literal notranslate"><span class="pre">model_name</span></code> 属性的对象，通常由当前模型的管理视图提供。</p>
</div>
<div class="section" id="s-the-display-decorator">
<span id="the-display-decorator"></span><h2><code class="docutils literal notranslate"><span class="pre">display</span></code> 装饰器<a class="headerlink" href="#the-display-decorator" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.contrib.admin.display">
<code class="descname">display</code>(<em>*</em>, <em>boolean=None</em>, <em>ordering=None</em>, <em>description=None</em>, <em>empty_value=None</em>)<a class="headerlink" href="#django.contrib.admin.display" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>这个装饰器可以用来设置自定义显示函数的特定属性，可以用 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.list_display" title="django.contrib.admin.ModelAdmin.list_display"><code class="xref py py-attr docutils literal notranslate"><span class="pre">list_display</span></code></a> 或 <a class="reference internal" href="#django.contrib.admin.ModelAdmin.readonly_fields" title="django.contrib.admin.ModelAdmin.readonly_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">readonly_fields</span></code></a> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@admin</span><span class="o">.</span><span class="n">display</span><span class="p">(</span>
    <span class="n">boolean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">ordering</span><span class="o">=</span><span class="s1">&#39;-publish_date&#39;</span><span class="p">,</span>
    <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Is Published?&#39;</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">def</span> <span class="nf">is_published</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">publish_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
</pre></div>
</div>
<p>这就相当于直接在函数上设置一些属性（用原来的、较长的名字）：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">is_published</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">publish_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
<span class="n">is_published</span><span class="o">.</span><span class="n">boolean</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">is_published</span><span class="o">.</span><span class="n">admin_order_field</span> <span class="o">=</span> <span class="s1">&#39;-publish_date&#39;</span>
<span class="n">is_published</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s1">&#39;Is Published?&#39;</span>
</pre></div>
</div>
<p>还请注意，<code class="docutils literal notranslate"><span class="pre">empty_value</span></code> 装饰符参数映射到直接分配给函数的 <code class="docutils literal notranslate"><span class="pre">empty_value_display</span></code> 属性。它不能与 <code class="docutils literal notranslate"><span class="pre">boolean</span></code> 一起使用 —— 它们是相互排斥的。</p>
<p>使用这个装饰器并不是制作一个显示函数的必经之路，但在你的源码中使用它而不使用参数作为标记来识别函数的目的是很有用的：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@admin</span><span class="o">.</span><span class="n">display</span>
<span class="k">def</span> <span class="nf">published_year</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">publish_date</span><span class="o">.</span><span class="n">year</span>
</pre></div>
</div>
<p>在这种情况下，它将不会给函数添加任何属性。</p>
</dd></dl>

</div>
<div class="section" id="s-the-staff-member-required-decorator">
<span id="the-staff-member-required-decorator"></span><h2><code class="docutils literal notranslate"><span class="pre">staff_member_required</span></code> 装饰器<a class="headerlink" href="#the-staff-member-required-decorator" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.contrib.admin.views.decorators.staff_member_required">
<code class="descname">staff_member_required</code>(<em>redirect_field_name='next'</em>, <em>login_url='admin:login'</em>)<a class="headerlink" href="#django.contrib.admin.views.decorators.staff_member_required" title="永久链接至目标">¶</a></dt>
<dd><p>这个装饰器被用于需要授权的管理视图。用这个函数装饰的视图将有以下行为：</p>
<ul class="simple">
<li>如果用户已登录，是工作人员（<code class="docutils literal notranslate"><span class="pre">User.is_staff=True</span></code>），并且是活动的（<code class="docutils literal notranslate"><span class="pre">User.is_active=True</span></code>），则正常执行视图。</li>
<li>否则，请求将被重定向到由 <code class="docutils literal notranslate"><span class="pre">login_url</span></code> 参数指定的 URL，并在由 <code class="docutils literal notranslate"><span class="pre">redirect_field_name</span></code> 指定的查询字符串变量中包含最初请求的路径。例如： <code class="docutils literal notranslate"><span class="pre">/admin/login/?next=/admin/polls/question/3/</span></code>。</li>
</ul>
<p>用法示例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.admin.views.decorators</span> <span class="kn">import</span> <span class="n">staff_member_required</span>

<span class="nd">@staff_member_required</span>
<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django 管理站点</a><ul>
<li><a class="reference internal" href="#overview">概况</a><ul>
<li><a class="reference internal" href="#other-topics">其他主题</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-objects"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 对象</a><ul>
<li><a class="reference internal" href="#the-register-decorator"><code class="docutils literal notranslate"><span class="pre">register</span></code> 装饰器</a></li>
<li><a class="reference internal" href="#discovery-of-admin-files">发现管理文件</a></li>
<li><a class="reference internal" href="#modeladmin-options"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 选项</a><ul>
<li><a class="reference internal" href="#custom-template-options">自定义模板选项</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-methods"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 方法</a><ul>
<li><a class="reference internal" href="#other-methods">其他方法</a></li>
</ul>
</li>
<li><a class="reference internal" href="#modeladmin-asset-definitions"><code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> 静态资源定义</a><ul>
<li><a class="reference internal" href="#jquery">jQuery</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-custom-validation-to-the-admin">在管理中添加自定义验证</a></li>
</ul>
</li>
<li><a class="reference internal" href="#inlinemodeladmin-objects"><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 对象</a><ul>
<li><a class="reference internal" href="#inlinemodeladmin-options"><code class="docutils literal notranslate"><span class="pre">InlineModelAdmin</span></code> 选项</a></li>
<li><a class="reference internal" href="#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model">与有两个或更多外键到同一父模型的模型一起工作</a></li>
<li><a class="reference internal" href="#working-with-many-to-many-models">与多对多模型一起工作</a></li>
<li><a class="reference internal" href="#working-with-many-to-many-intermediary-models">与多对多中间模型一起工作</a></li>
<li><a class="reference internal" href="#using-generic-relations-as-an-inline">使用通用关系作为内联</a></li>
</ul>
</li>
<li><a class="reference internal" href="#overriding-admin-templates">覆盖管理模板</a><ul>
<li><a class="reference internal" href="#set-up-your-projects-admin-template-directories">设置你的项目管理模板目录</a></li>
<li><a class="reference internal" href="#overriding-vs-replacing-an-admin-template">覆盖 vs. 替换管理模板</a></li>
<li><a class="reference internal" href="#templates-which-may-be-overridden-per-app-or-model">每个应用或模型可覆盖的模板</a></li>
<li><a class="reference internal" href="#root-and-login-templates">根和登录模板</a></li>
</ul>
</li>
<li><a class="reference internal" href="#theming-support">主题化支持</a></li>
<li><a class="reference internal" href="#adminsite-objects"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 对象</a><ul>
<li><a class="reference internal" href="#adminsite-attributes"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 属性</a></li>
<li><a class="reference internal" href="#adminsite-methods"><code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 方法</a></li>
<li><a class="reference internal" href="#hooking-adminsite-instances-into-your-urlconf">将 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 实例挂到你的 URLconf 中</a></li>
<li><a class="reference internal" href="#customizing-the-adminsite-class">自定义 <code class="docutils literal notranslate"><span class="pre">AdminSite</span></code> 类</a></li>
<li><a class="reference internal" href="#overriding-the-default-admin-site">覆盖默认的管理站点</a></li>
<li><a class="reference internal" href="#multiple-admin-sites-in-the-same-urlconf">同一个 URLconf 中的多个管理站点</a></li>
<li><a class="reference internal" href="#adding-views-to-admin-sites">在管理站点上添加视图</a></li>
<li><a class="reference internal" href="#adding-a-password-reset-feature">增加密码重置功能</a></li>
</ul>
</li>
<li><a class="reference internal" href="#logentry-objects"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 对象</a><ul>
<li><a class="reference internal" href="#logentry-attributes"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 属性</a></li>
<li><a class="reference internal" href="#logentry-methods"><code class="docutils literal notranslate"><span class="pre">LogEntry</span></code> 方法</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reversing-admin-urls">反查管理 URL</a></li>
<li><a class="reference internal" href="#the-display-decorator"><code class="docutils literal notranslate"><span class="pre">display</span></code> 装饰器</a></li>
<li><a class="reference internal" href="#the-staff-member-required-decorator"><code class="docutils literal notranslate"><span class="pre">staff_member_required</span></code> 装饰器</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="../index.html"
                        title="上一章"><code class="docutils literal notranslate"><span class="pre">contrib</span></code> 包</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="actions.html"
                        title="下一章">管理动作</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/admin/index.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;contrib&lt;/span&gt;&lt;/code&gt; 包">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="actions.html" title="管理动作">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>